2012-09-28 63 views
0

我有一个列表元组列表和2项列表。我需要合并并创建一个新的字典。合并元组和列表的列表,并创建字典

下面是一个例子: 输入:

list1 = [('col1',20,30),('col2',40,50)] 
list2 = ['name','age'] 

期望的结果:

output = {'col1':'name','col2':'age'} 

回答

3
output = {el1[0]: el2 for el1, el2 in zip(list1, list2)} 

还是老版本的Python:

output = dict((el1[0], el2) for el1, el2 in zip(list1, list2)) 
+0

我得到一个语法错误 – user1050619

+1

@ user1050619运行好的Python 2.7。您发布的代码有错字。 'list1'的定义有一个额外的右括号。 – chucksmash

+0

lol..it working :) .. ty – user1050619