Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将列表和列表列表转换为dict_Python_List_Python 2.7_Dictionary - Fatal编程技术网

在python中将列表和列表列表转换为dict

在python中将列表和列表列表转换为dict,python,list,python-2.7,dictionary,Python,List,Python 2.7,Dictionary,我有两个列表['a',b','c']和[[1,2,3],[4,5,6] 我希望输出{'a':[1,4],'b':[2,5],'c':[3,6]}而不使用for循环。使用: 更新 如果要获取字符串列表映射,请使用: 正如在另一个答案中所说的,您可能应该使用zip。但是,如果您想避免使用其他第三方库,您可以通过在每个元素上调用for循环并手动将其添加到字典中来手动执行此操作。不使用for循环 list1 = ['a', 'b', 'c'] list2 = [[1,2,3], [4,5,6]] fl

我有两个列表
['a',b','c']
[[1,2,3],[4,5,6]

我希望输出
{'a':[1,4],'b':[2,5],'c':[3,6]}
而不使用for循环。

使用:


更新

如果要获取字符串列表映射,请使用:


正如在另一个答案中所说的,您可能应该使用zip。但是,如果您想避免使用其他第三方库,您可以通过在每个元素上调用for循环并手动将其添加到字典中来手动执行此操作。

不使用for循环

list1 = ['a', 'b', 'c']
list2 = [[1,2,3], [4,5,6]]
flat = reduce(lambda x,y: x+y,list2)
d = {}
df = dict(enumerate(flat))

def create_dict(n):
  position = flat.index(df[n])%len(list1)
  if list1[position] in d.keys():
     d[list1[position]].append(df[n])
  else:
     d[list1[position]] = [df[n]]

map( create_dict, df)
print d

非常感谢你的回答。@BurhanKhalid,你说得对。我更新了答案,添加了一个返回字符串列表映射的不同版本。谢谢您指出。@user3355648:如果您认为此答案有帮助,请记住将此答案标记为已接受。请参阅:
>>> {key:list(value) for key, value in zip(l1, zip(*l2))}
{'a': [1, 4], 'b': [2, 5], 'c': [3, 6]}
list1 = ['a', 'b', 'c']
list2 = [[1,2,3], [4,5,6]]
flat = reduce(lambda x,y: x+y,list2)
d = {}
df = dict(enumerate(flat))

def create_dict(n):
  position = flat.index(df[n])%len(list1)
  if list1[position] in d.keys():
     d[list1[position]].append(df[n])
  else:
     d[list1[position]] = [df[n]]

map( create_dict, df)
print d