0
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"]
  }

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

4

1 回答 1

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

izip如果列表较长,imap则 , 等可能是合适的。

于 2013-08-29T04:22:24.903 回答