有序字典
- 与正常字典一致,只是会保留插入时的顺序
- 创建有序字典
| 不过由于多了保留顺序的功能,因此在使用可迭代对象创建有序字典时, 可以先对它排个序,让创建出来的字典元素也是有序的: |
|---|
import collections
data = [('a', 1), ('b', 3), ('c', 2)]
od = collections.OrderedDict(sorted(data, key = lambda s:s[0]))
print(od)
od = collections.OrderedDict(sorted(data, key = lambda s:s[1]))
print(od)
输出:

dt = collections.OrderedDict()
dt['a'] = 0
dt['b'] = 1
dt['c'] = 2
dt.move_to_end('b', last= False)
print(dt)
dt.move_to_end('b',last=True)
print(dt)
输出

import collections
def Fun_Dict():
Dict = []
n = int(input())
for h in range(n):
data = input()
Dict.append((data, h))
od = collections.OrderedDict(sorted(Dict, key = lambda s:s[0]))
print(od)
1308

被折叠的 条评论
为什么被折叠?



