如何在字典中添加两个列表?

时间:2013-08-29 04:18:43

标签: python dictionary

i=["Pin","Type","value"]
j=[["abc","input","1234"],["xyz","output","2345"],["pqr","input","567"]]
z=dict(zip(i,j))

我想将它们加入到字典中,以便我的输出应该是这样的,

z={
   "Pin": ["abc","xyz","pqr"],
   "Type": ["input","input","input"],
   "value": ["1234","2345","567"]
  }

如何将这两个列表添加到字典中?

1 个答案:

答案 0 :(得分:2)

>>> dict(zip(i,zip(*j)))
{'Type': ('input', 'output', 'input'), 'value': ('1234', '2345', '567'), 'Pin': ('abc', 'xyz', 'pqr')}

或者如果你真的想要名单,

>>> dict(zip(i,map(list,zip(*j))))
{'Type': ['input', 'output', 'input'], 'value': ['1234', '2345', '567'], 'Pin': ['abc', 'xyz', 'pqr']}
如果列表更长,

izipimap等可能是合适的。