0
my_lst = ['b','c','a']
dict = {}
for i in my_lst[]:
    dict[i] = []
print dict
# prints: => {'c': [], 'b': [], 'a': []}

我怎样才能让 dict 以{'b': [ ], 'c': [ ], 'a': [ ]} 与列表中类似的顺序使用键?

4

1 回答 1

3

您正在寻找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', [])]
于 2013-11-13T00:22:21.323 回答