【问题标题】:Update list items inside a list (matrix)更新列表(矩阵)中的列表项
【发布时间】: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


    【解决方案1】:

    我想这就是你现在所拥有的

    all_words = [ {'definition': 'Maladie virale caracterisee par une eruption de vesicules '
        ...:                 'disposees sur le trajet des nerfs sensitifs.',
        ...:   'word': 'ZONA',
        ...:   'word_length': Decimal('4')},
        ...:  {'definition': "Partie d'une surface spherique comprise entre deux plans "
        ...:                 'paralleles.',
        ...:   'word': 'ZONE',
        ...:   'word_length': Decimal('4')},
        ...:  {'definition': 'Musique de danse tres rythmee, originaire de la Martinique.',
        ...:   'word': 'ZOUK',
        ...:   'word_length': Decimal('4')},
        ...:  {'definition': 'Naif, niais.', 'word': 'ZOZO', 'word_length': Decimal('4')}]
    

    首先得到一个只有正确长度的单词的列表

    only_words = [word["word"] for word in all_words if len(word["word"]) == 4]
    
    

    然后使用 list() 将单词变成单个字母的列表

    [list(word) for word in only_words[:4]]
    
    [['Z', 'O', 'N', 'A'],
     ['Z', 'O', 'N', 'E'],
     ['Z', 'O', 'U', 'K'],
     ['Z', 'O', 'Z', 'O']]
    

    编辑:我在第一个列表 comp 中添加了一个 if 语句,以按单词长度过滤单词。我看到你有一个“word_length”键,但我不熟悉 Decimal() 的东西。

    在第二个列表组合中,我使用列表切片仅获取前 n 个单词

    【讨论】:

    • 嗨,爱德华感谢您的回答,它确实有效,但部分与我的问题有关。我想我的字典不清楚,我有很多,不仅是 4 个。所以你的解决方案有效,但我有一个包含所有单词的庞大矩阵(大约 1,000 个)。您是否有一种解决方法来获取与我的矩阵大小匹配的单词数?这里我的矩阵是 4X4,所以我只需要 4 个单词。但是假设我的矩阵是 8X8,那么我需要 8 个单词来填充我的矩阵
    • 我做了一个编辑,我认为这就是你要找的。基本上只是按长度过滤掉单词,然后取前n个单词组成矩阵。
    【解决方案2】:

    您可以直接使用list() 将单词类型转换为列表以创建二维列表,而不是替换已创建列表中的哈希。

    all_words = ['ZONA' , 'ZONE', 'ZOUK', 'ZOZO']
    print([list(word) for word in all_words])
    

    输出:

    [['Z', 'O', 'N', 'A'], ['Z', 'O', 'N', 'E'], ['Z', 'O', 'U', 'K'], ['Z', 'O', 'Z', 'O']]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 2017-05-03
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2018-01-03
      相关资源
      最近更新 更多