多变量列表理解python

多变量列表理解python,python,list-comprehension,Python,List Comprehension,我正在尝试迭代多个对象。如何使用列表理解重写它 for character in characters_rs: for item in mapping: if character.Get('NomId') == item.get('Номенклатура'): item['Attributes'] = character.Get('Attributes') 我尝试了类似于aggregated_list=[character for charac

我正在尝试迭代多个对象。如何使用列表理解重写它

for character in characters_rs:
    for item in mapping:
        if character.Get('NomId') == item.get('Номенклатура'):
            item['Attributes'] = character.Get('Attributes')
我尝试了类似于
aggregated_list=[character for character in characters\u rs for item in mapping if character.Get('NomId')==item.Get('href\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'a')]
,但这不是我所期望的


映射是一个列表,字符是一个自定义记录集结构。

您可以交换这两个循环,因为它们是独立的。这使过程更加清晰:您希望更新
映射的
项的一些
项:

for item in mapping:
    for character in characters_rs:
        if character.Get('NomId') == item.get('Номенклатура'):
            item['Attributes'] = character.Get('Attributes')
更准确地说,您可以使用具有
character.Get('NomId')==item.Get('öМааааа')
的最后一个
字符更新每个
项。因此,返回
映射更新版本的list/dict理解:

[{**{'Attributes': character.Get('Attributes')
     for character in characters_rs
     if character.Get('NomId') == item.get('Номенклатура')},
  **item}
 for item in mapping]

说明:
{**d1,**d2}
合并了两条指令。这里,第一个dict包含最后一个字符,该字符具有映射到
属性的
character.Get('NomId')==item.Get('href\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,因此,您根本不应该使用列表理解。在列表理解中不能有赋值语句。
聚合列表
与第一个代码段有什么关系?什么类型的数据是
character.Get('NomId')
item.Get('href\\\\\\\\\\\\\\\\\\\\\\\\'code>)
-它们是整数、字符串还是其他?