将值添加到嵌套列表内的嵌套字典中

时间:2019-10-17 14:31:52

标签: python list dictionary

我有一个列表,其中包含列表。此列表包含字典,而此字典包含字典本身(列表->列表->字典->字典)。

数据看起来像这样(只是一小部分,我不仅有3个这样的列表,还有1500个“第二级”列表,这些列表通常包含大约5-15个字典,其中包含(总是)14个字典;此外,列表中的字典与列表中的字典不同,如下所示:

nest = [[{'Dict11': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict12': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict13': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict22': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict23': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict32': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict33': {7: 'C1', 8: 'C2', 9:'C3'}}],
     [{ 'Dict11': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict12': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict13': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict22': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict23': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict32': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict33': {7: 'C1', 8: 'C2', 9:'C3'}}],
     [{ 'Dict11': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict12': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict13': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict22': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict23': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict32': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict33': {7: 'C1', 8: 'C2', 9:'C3'}}]]

我有第二个列表,其长度与我的列表的长度相同(在此示例中为3,但在数据中为1500)。

val = [100, 200, 300]

我想将此列表(val)中的值添加到嵌套字典中。因此,第二个列表的第一个值应该出现在第一个列表的每个子词典中,第二个列表的第二个值应该出现在第二个列表的每个子词典中,等等。

最后基本上应该看起来像这样:

nest = [[{'Dict11': {1: 'A1', 2: 'A2', 3:'A3', 'id': 100},  
    'Dict12': {4: 'B1', 5: 'B2', 6:'B3', 'id': 100},
    'Dict13': {7: 'C1', 8: 'C2', 9:'C3', 'id': 100}},
  { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3', 'id': 100},  
    'Dict22': {4: 'B1', 5: 'B2', 6:'B3', 'id': 100},
    'Dict23': {7: 'C1', 8: 'C2', 9:'C3', 'id': 100}},
  { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3', 'id': 100},  
    'Dict32': {4: 'B1', 5: 'B2', 6:'B3', 'id': 100},
    'Dict33': {7: 'C1', 8: 'C2', 9:'C3', 'id': 100}}],
 [{ 'Dict11': {1: 'A1', 2: 'A2', 3:'A3', 'id': 200},  
    'Dict12': {4: 'B1', 5: 'B2', 6:'B3', 'id': 200},
    'Dict13': {7: 'C1', 8: 'C2', 9:'C3', 'id': 200}},
  { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3', 'id': 200},  
    'Dict22': {4: 'B1', 5: 'B2', 6:'B3', 'id': 200},
    'Dict23': {7: 'C1', 8: 'C2', 9:'C3', 'id': 200}},
  { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3', 'id': 200},  
    'Dict32': {4: 'B1', 5: 'B2', 6:'B3', 'id': 200},
    'Dict33': {7: 'C1', 8: 'C2', 9:'C3', 'id': 200}}],
 [{ 'Dict11': {1: 'A1', 2: 'A2', 3:'A3', 'id': 300},  
    'Dict12': {4: 'B1', 5: 'B2', 6:'B3', 'id': 300},
    'Dict13': {7: 'C1', 8: 'C2', 9:'C3', 'id': 300}},
  { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3', 'id': 300},  
    'Dict22': {4: 'B1', 5: 'B2', 6:'B3', 'id': 300},
    'Dict23': {7: 'C1', 8: 'C2', 9:'C3', 'id': 300}},
  { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3', 'id': 300},  
    'Dict32': {4: 'B1', 5: 'B2', 6:'B3', 'id': 300},
    'Dict33': {7: 'C1', 8: 'C2', 9:'C3', 'id': 300}}]]

我尝试过

for sublist in nest:
sublist.append(val)

但是那绝对不能像我想要的那样工作。

如何将第二个列表中的值添加到嵌套列表的嵌套字典中? 谢谢!

2 个答案:

答案 0 :(得分:2)

您可以对zip使用列表推导:

val = [100, 200, 300]
nest = [[{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict12': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict13': {7: 'C1', 8: 'C2', 9: 'C3'}}, {'Dict21': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict22': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict23': {7: 'C1', 8: 'C2', 9: 'C3'}}, {'Dict31': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict32': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict33': {7: 'C1', 8: 'C2', 9: 'C3'}}], [{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict12': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict13': {7: 'C1', 8: 'C2', 9: 'C3'}}, {'Dict21': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict22': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict23': {7: 'C1', 8: 'C2', 9: 'C3'}}, {'Dict31': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict32': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict33': {7: 'C1', 8: 'C2', 9: 'C3'}}], [{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict12': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict13': {7: 'C1', 8: 'C2', 9: 'C3'}}, {'Dict21': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict22': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict23': {7: 'C1', 8: 'C2', 9: 'C3'}}, {'Dict31': {1: 'A1', 2: 'A2', 3: 'A3'}, 'Dict32': {4: 'B1', 5: 'B2', 6: 'B3'}, 'Dict33': {7: 'C1', 8: 'C2', 9: 'C3'}}]]
new_nest = [[{c:{**d, 'id':a} for c, d in i.items()} for i in b] for a, b in zip(val, nest)]

输出:

[[{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 100}, 
  'Dict12': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 100}, 
  'Dict13': {7: 'C1', 8: 'C2', 9: 'C3', 'id': 100}}, 
  {'Dict21': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 100}, 
  ...]
 [{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 200}, 
  'Dict12': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 200}, 
  'Dict13': {7: 'C1', 8: 'C2', 9: 'C3', 'id': 200}}, 
  {'Dict21': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 200}, 
   'Dict22': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 200}, 
  ...]
 [{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 300}, 
   'Dict12': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 300}, 
   'Dict13': {7: 'C1', 8: 'C2', 9: 'C3', 'id': 300}}, 
    {'Dict21': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 300}, 
   'Dict22': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 300},
  ....}]]

答案 1 :(得分:1)

此解决方案可以操纵“巢”本身,因此应具有很高的性能,并且不需要根据需要增加空间。

nest = [[{'Dict11': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict12': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict13': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict22': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict23': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict32': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict33': {7: 'C1', 8: 'C2', 9:'C3'}}],
     [{ 'Dict11': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict12': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict13': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict22': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict23': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict32': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict33': {7: 'C1', 8: 'C2', 9:'C3'}}],
     [{ 'Dict11': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict12': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict13': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict21': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict22': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict23': {7: 'C1', 8: 'C2', 9:'C3'}},
      { 'Dict31': {1: 'A1', 2: 'A2', 3:'A3'},  
        'Dict32': {4: 'B1', 5: 'B2', 6:'B3'},
        'Dict33': {7: 'C1', 8: 'C2', 9:'C3'}}]]

val = [100, 200, 300]

for i in range(0, len(nest)):
    for y in nest[i]:
        for value in y.values():
            value['id'] = val[i]

print(nest)
# Result:
[[
{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 100},
'Dict12': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 100},
'Dict13': {8: 'C2', 9: 'C3', 'id': 100, 7: 'C1'}},
{'Dict21': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 100},
'Dict22': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 100},
'Dict23': {8: 'C2', 9: 'C3', 'id': 100, 7: 'C1'}},
{'Dict31': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 100},
'Dict33': {8: 'C2', 9: 'C3', 'id': 100, 7: 'C1'},
'Dict32': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 100}}],
[{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 200},
'Dict12': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 200},
'Dict13': {8: 'C2', 9: 'C3', 'id': 200, 7: 'C1'}},
{'Dict21': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 200},
'Dict22': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 200},
'Dict23': {8: 'C2', 9: 'C3', 'id': 200, 7: 'C1'}},
{'Dict31': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 200},
'Dict33': {8: 'C2', 9: 'C3', 'id': 200, 7: 'C1'},
'Dict32': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 200}}],
[{'Dict11': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 300},
'Dict12': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 300},
'Dict13': {8: 'C2', 9: 'C3', 'id': 300, 7: 'C1'}},
{'Dict21': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 300},
'Dict22': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 300},
'Dict23': {8: 'C2', 9: 'C3', 'id': 300, 7: 'C1'}},
{'Dict31': {1: 'A1', 2: 'A2', 3: 'A3', 'id': 300},
'Dict33': {8: 'C2', 9: 'C3', 'id': 300, 7: 'C1'},
'Dict32': {4: 'B1', 5: 'B2', 6: 'B3', 'id': 300}}
]]