比方說,有一本字典Python的排序由Index
foo = {'b': 1, 'c':2, 'a':3 }
我想遍歷這個字典在字典中的項目的出場順序。
for k,v in foo.items():
print k, v
打印
a 3
c 2
b 1
,如果我們使用排序()函數:
for k,v in sorted(foo.items()):
print k, v
打印
a 3
b 1
c 2
但我需要他們在它們出現的順序字典我; e
b 1
c 2
a 3
我該如何做到這一點?
['OrderedDict'](HTTP ://docs.python.org/library/collections.html#collections.OrderedDict) –