python - 仅当特定数字位于该位置时才更改嵌套列表中的值
问题描述
我有这个宾果板,我还有一个随机生成的列表,里面有 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,但中奖号码周围带有“[]”。
解决方案
尝试这个:
print(['['+i+']' if i in winning_numbers else i for i in bingo_board])
推荐阅读
- android - onCreate、onStart()、onResume()、onDestroy()的源代码是什么?
- twitter-bootstrap - twitter-bootstrap(从 bootstrap.css 中删除未使用的代码)
- php - Windows 10 上的 Memcache PHP 扩展间歇性工作
- express - 续集插入到多个表中
- html - 具有多个条件的 ngClass 错误条件不起作用
- python - 如何异步迭代范围
- java - Mybatis 3.4.6 - 数据未插入 HSQLDB
- shell - 如果为真,Shell 脚本从某些给定行打印整行
- excel - 如果单元格中的值与前一个单元格不同,如何插入 1,如果值相同,如何插入 2
- python-3.x - 在 Pandas 中将行转换为列和一种热编码