dict={}
i=["abc","def","ghi","jkl"]
j=[["a","b","c","d"],["q","w","e","r"],["t","y","u","i"]]
for item in i:
dict[item]=[str(j[item])]
print dict
输出应该像
dict={"abc":["a","b","c","d"], "def":["q","w","e","r"] ...}
如何在python中将列表添加到字典中?
dict={}
i=["abc","def","ghi","jkl"]
j=[["a","b","c","d"],["q","w","e","r"],["t","y","u","i"]]
for item in i:
dict[item]=[str(j[item])]
print dict
输出应该像
dict={"abc":["a","b","c","d"], "def":["q","w","e","r"] ...}
如何在python中将列表添加到字典中?
使用zip()
函数来组合两个列表:
dict(zip(i, j))
演示:
>>> i=["abc","def","ghi","jkl"]
>>> j=[["a","b","c","d"],["q","w","e","r"],["t","y","u","i"]]
>>> dict(zip(i, j))
{'abc': ['a', 'b', 'c', 'd'], 'ghi': ['t', 'y', 'u', 'i'], 'def': ['q', 'w', 'e', 'r']}
zip()
将列表中的元素配对成一个元组序列;构造函数接受一个元组序列并将dict()
它们解释为键值对。