如何在字典中添加两个列表? [英] How to add two lists into dictionary?
本文介绍了如何在字典中添加两个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i=["Pin","Type","value"]
j=[["abc","input","1234"],["xyz","output","2345"],["pqr","input","567"]]
z=dict(zip(i,j))
我想将它们加入字典,这样我的输出应该是这样的,
And I want to join them into dictionary, so that my output should be like this,
z={
"Pin": ["abc","xyz","pqr"],
"Type": ["input","input","input"],
"value": ["1234","2345","567"]
}
如何将这两个列表添加到字典中?
How can I add this two lists into dictionary?
推荐答案
>>> dict(zip(i,zip(*j)))
{'Type': ('input', 'output', 'input'), 'value': ('1234', '2345', '567'), 'Pin': ('abc', 'xyz', 'pqr')}
或者,如果您真的想要列表,
Or if you really want lists,
>>> dict(zip(i,map(list,zip(*j))))
{'Type': ['input', 'output', 'input'], 'value': ['1234', '2345', '567'], 'Pin': ['abc', 'xyz', 'pqr']}
izip
,
izip
, imap
, etc may be appropriate if the lists were longer.
这篇关于如何在字典中添加两个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文