2016-06-11 93 views
2

我有三個列表,裏面有多個字典。從Python中的多個列表中合併字典

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

回答

2

zip列表中的字典項目。變換的字典成鍵值tuples列表,合併使用+的名單,然後變換合併列表回的字典:

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

在蟒蛇3.x中,你將需要調用listdict_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'}] 

字典項目是沒有順序的,所以每個字典可不要進入問題答案 - 糾正答案訂單。順序可能不同。

+0

AttributeError的:「海峽」對象有沒有屬性「項目」是你的代碼的結果是,我使用python2 –

+0

@DeanChristianArmada這個答案是假設列表的-字典問題的方式,而不是列表OF-你正在嘗試的字符串。 – ppperry

+0

@DeanChristianArmada此方法使用字典列表 –

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?'}] 
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)] 

但我想我的主要答案看起來更清潔(更少的系列括號)。

+0

哇,這也是一個很好的答案! –

相關問題