0
我有一个列表元组列表和2项列表。我需要合并并创建一个新的字典。合并元组和列表的列表,并创建字典
下面是一个例子: 输入:
list1 = [('col1',20,30),('col2',40,50)]
list2 = ['name','age']
期望的结果:
output = {'col1':'name','col2':'age'}
我有一个列表元组列表和2项列表。我需要合并并创建一个新的字典。合并元组和列表的列表,并创建字典
下面是一个例子: 输入:
list1 = [('col1',20,30),('col2',40,50)]
list2 = ['name','age']
期望的结果:
output = {'col1':'name','col2':'age'}
output = {el1[0]: el2 for el1, el2 in zip(list1, list2)}
还是老版本的Python:
output = dict((el1[0], el2) for el1, el2 in zip(list1, list2))
我得到一个语法错误 – user1050619
@ user1050619运行好的Python 2.7。您发布的代码有错字。 'list1'的定义有一个额外的右括号。 – chucksmash
lol..it working :) .. ty – user1050619