【发布时间】:2022-01-03 14:41:29
【问题描述】:
我正在尝试更新列表中的所有列表项。最好说它是一个矩阵。我正在这样构建它:
grids = [["#"] * grid_size for _ in range(grid_size)]
如果我的网格尺寸为 4,则输出:
[['#', '#', '#', '#'],
['#', '#', '#', '#'],
['#', '#', '#', '#'],
['#', '#', '#', '#']]
在旁边,我有一个字典列表,里面有几个单词。代码:all_words = [x for x in words]
所有单词的输出:
...
...
{'definition': 'Maladie virale caractérisée par une éruption de vésicules '
'disposées sur le trajet des nerfs sensitifs.',
'word': 'ZONA',
'word_length': Decimal('4')},
{'definition': "Partie d'une surface sphérique comprise entre deux plans "
'parallèles.',
'word': 'ZONE',
'word_length': Decimal('4')},
{'definition': 'Musique de danse très rythmée, originaire de la Martinique.',
'word': 'ZOUK',
'word_length': Decimal('4')},
{'definition': 'Naïf, niais.', 'word': 'ZOZO', 'word_length': Decimal('4')}]
我想做的是替换矩阵中的“#”,以添加我字典中的“单词”。这里的例子是“ZONA”、“ZONE”、“ZOUK”和“ZOZO”,这是我最后的四个字。
期望的输出:
[['Z', 'O', 'N', 'A'],
['Z', 'O', 'N', 'E'],
['Z', 'O', 'U', 'K'],
['Z', 'O', 'Z', 'O']]
最好的当然是只添加这四个单词,这样矩阵就不会扩展得更多。我尝试在另一个列表理解中使用列表理解,但我把一切都搞砸了......
非常感谢您的帮助! 电视-
【问题讨论】:
标签: python list list-comprehension