从3个列表的zip中创建字典 [英] Сreate a dictionary from a zip of 3 lists
本文介绍了从3个列表的zip中创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在字典中列出3个列表:
I have 3 lists that I would like to put into a dictionary:
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
list3 = [0.5, 0.3, 0.1]
传统上,我可以只用list1
,list2
Traditionally I could create a dictionary like this with just list1
, list2
my_dict = dict(zip(list1, list2))
# {'a': 1, 'b': 2, 'c': 3}
但是我想得到的是:
{'a': (1, 0.5), 'b': (2, 0.3), 'c': (3, 0.1)}
这不起作用:
my_dict = dict(list1, zip(list2, list3))
推荐答案
您需要再添加一个zip,因为dict
构造函数接受tuple
的列表,但不接受两个list
的列表:
You need to add one more zip, since dict
constructor accepts list of tuple
s, but not two list
s:
my_dict_3 = dict(zip(list1, zip(list2, list3)))
这篇关于从3个列表的zip中创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文