首页 > 解决方案 > 仅当特定数字位于该位置时才更改嵌套列表中的值

问题描述

我有这个宾果板,我还有一个随机生成的列表,里面有 10 个中奖号码

bingo_board = [
        [" 1", " 2", " 3", " 4", " 5"],
        [" 6", " 7", " 8", " 9", "10"],
        ["11", "12", "13", "14", "15"],
        ["16", "17", "18", "19", "20"],
        ["21", "22", "23", "24", "25"]
    ]

#converts bingo nested list to regular list, then randomizes 10 numbers form that list
numbers = [ n for numbers in bingo_board for n in numbers]
    winning_numbers = random.sample(numbers,10)

我想要做的是再次打印 bingo_board,但中奖号码周围带有“[]”。

标签: pythonnested-lists

解决方案


尝试这个:

print(['['+i+']' if i in winning_numbers else i for i in bingo_board])

推荐阅读