我有两个列表:
i = ['a', 'b', 'c']
x = [1,2,3]
我需要制作这样的字典:
xxx = { 'a': [a, 1],
'b': [b, 2],
'c': [c, 3]}
我这样做了:
for indx in i:
for indx2 in x:
xxx.update({indx: [indx, indx2]})
但显然它不起作用
最佳答案
使用字典理解:
>>> i = ['a', 'b', 'c']
>>> x = [1,2,3]
>>> {key: [key, value] for key, value in zip(i, x)}
{'a': ['a', 1], 'c': ['c', 3], 'b': ['b', 2]}
关于python - 如何按索引合并两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25991640/