my_lst = ['b','c','a']
dict = {}
for i in my_lst[]:
dict[i] = []
print dict
# prints: => {'c': [], 'b': [], 'a': []}
我怎样才能让 dict 以{'b': [ ], 'c': [ ], 'a': [ ]}
与列表中类似的顺序使用键?
my_lst = ['b','c','a']
dict = {}
for i in my_lst[]:
dict[i] = []
print dict
# prints: => {'c': [], 'b': [], 'a': []}
我怎样才能让 dict 以{'b': [ ], 'c': [ ], 'a': [ ]}
与列表中类似的顺序使用键?
您正在寻找OrderedDict
:
>>> from collections import OrderedDict
>>> my_lst = ['b','c','a']
>>> D = OrderedDict()
>>> for i in my_lst:
... D[i] = []
>>> D
OrderedDict([('b', []), ('c', []), ('a', [])])
>>> print D.items()
[('b', []), ('c', []), ('a', [])]