在Python中合并多个列表中的词典

时间:2016-06-11 01:18:55

标签: python dictionary

我有三个包含多个词典的列表。

list1 = [{'question': u'Fan offline information can be found on what Screen under General Menu? '}, {'question': u'What is the tool for F5 BIGIP to get packet traces. '}, {'question': u'On a HTTP Health Monitor configuration. If Receive string and Disabling string matched and Reverse is enabled. What would be the status of pool members?'}]
list2 = [{'answer': u'SysteminfoScreen'}, {'answer': u'qkview'}, {'answer': u'Offline'}]
list3 = [{'correct_answer': u'SysteminfoScreen'}, {'correct_answer': u'TCP Dump'}, {'correct_answer': u'Disabled'}]

如何将这三个列表合并为一个类似的结果?

[{'question': u'What is the tool for F5 BIGIP to get packet traces. ', 'answer': u'qkview', 'correct_answer': u'TCP Dump'}]

如果上述问题无法实现,则另一种选择

list1 = ['Fan offline information can be found on what Screen under General Menu? ', 'What is the tool for F5 BIGIP to get packet traces. ', 'On a HTTP Health Monitor configuration. If Receive string and Disabling string matched and Reverse is enabled. What would be the status of pool members?']
list2 = ['SysteminfoScreen', 'qkview', 'Offline']
list3 = ['SysteminfoScreen', 'TCP Dump', 'Disabled']

将三者合并为相同的结果:

[{'question': u'What is the tool for F5 BIGIP to get packet traces. ', 'answer': u'qkview', 'correct_answer': u'TCP Dump'}]

PS

我正在使用python 2.7.10

3 个答案:

答案 0 :(得分:2)

zip列表中的字典项。将字典转换为键值tuples列表,使用+合并列表,然后将合并列表转换回字典:

[dict(i.items()+j.items()+k.items()) for i, j, k in zip(list1, list2, list3)]

在python 3.x中,您需要在list上致电dict_items

[dict(list(i.items())+list(j.items())+list(k.items())) for i,j,k in zip(list1, list2, list3)]

结果:

[{'answer': 'SysteminfoScreen',
  'question': 'Fan offline information can be found on what Screen under General Menu? ',
  'correct_answer': 'SysteminfoScreen'},
 {'answer': 'qkview',
  'question': 'What is the tool for F5 BIGIP to get packet traces. ',
  'correct_answer': 'TCP Dump'},
 {'answer': 'Offline',
  'question': 'On a HTTP Health Monitor configuration. If Receive string and Disabling string matched and Reverse is enabled. What would be the status of pool members?',
  'correct_answer': 'Disabled'}]

字典项目未订购,因此每个字典可能不会进入Question-Answer-CorrectAnswer订单。订单可能不同。

答案 1 :(得分:2)

只需循环播放,保持简单易读:

res = []  # keep results

for vals in zip(list1, list2, list3):  # grab each entry 
    d = {}                             # tmp dictionary 
    for subv in vals: d.update(subd)   # update tmp dictionary
    res.append(d)                      # add to result

对于您的输入,此收益率:

[{'answer': 'SysteminfoScreen',
  'correct_answer': 'SysteminfoScreen',
  'question': 'Fan offline information can be found on what Screen under General Menu? '},
 {'answer': 'qkview',
  'correct_answer': 'TCP Dump',
  'question': 'What is the tool for F5 BIGIP to get packet traces. '},
 {'answer': 'Offline',
  'correct_answer': 'Disabled',
  'question': 'On a HTTP Health Monitor configuration. If Receive string and Disabling string matched and Reverse is enabled. What would be the status of pool members?'}]

答案 2 :(得分:1)

我认为以下内容对于您提供的第二个配置看起来非常不错。它需要一些变量重命名,但在我看来更清楚。

[dict(question=a, answer=b, correct_answer=c) for (a, b, c) in zip(list1, list2, list3)]

注:

上述解决方案也可以写成

[{'question': a, 'answer': b, 'correct_answer': c} for (a, b, c) in zip(list1, list2, list3)]

但我认为我的主要答案看起来更清晰(少数系列括号)。

相关问题