Cyb*_*uck 0 python dictionary list
我正在写这个问题尽管答案很多,stackoverflow因为解决方案对我的问题不起作用.
我有2个列表,List1并且List2.当我dict(zip(List1,List2))在字典中的元素的顺序被扰乱.
print s_key
print value
sorted_dict = {k: v for k,v in zip(s_key,value)}
another_test = dict(zip(s_key,value))
print sorted_dict
print another_test
print zip(s_key,value))
Run Code Online (Sandbox Code Playgroud)
终奌站 :
[2, 1, 3]
[31, 12, 5]
{1: 12, 2: 31, 3: 5}
{1: 12, 2: 31, 3: 5}
[(2, 31), (1, 12), (3, 5)]
Run Code Online (Sandbox Code Playgroud)
我的印象是,[(2, 31), (1, 12), (3, 5)]它将被转换为dict
任何帮助,以了解我在哪里或我做错了会有所帮助!谢谢!
a=[2, 1, 3]
b=[31, 12, 5]
from collections import OrderedDict
print(OrderedDict(zip(a,b)))
Run Code Online (Sandbox Code Playgroud)