jxn*_*jxn 5 python zip dictionary list
我有 3 个列表要放入字典中:
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
list3 = [0.5, 0.3, 0.1]
Run Code Online (Sandbox Code Playgroud)
传统上,我可以创建一个像这样的字典list1
,list2
my_dict = dict(zip(list1, list2))
# {'a': 1, 'b': 2, 'c': 3}
Run Code Online (Sandbox Code Playgroud)
但我想得到的是:
{'a': (1, 0.5), 'b': (2, 0.3), 'c': (3, 0.1)}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
my_dict = dict(list1, zip(list2, list3))
Run Code Online (Sandbox Code Playgroud)
您需要再添加一个 zip,因为dict
构造函数接受tuple
s列表,但不接受两个list
s:
my_dict_3 = dict(zip(list1, zip(list2, list3)))
Run Code Online (Sandbox Code Playgroud)