Python_collections_OrderedDict有序字典部分功能介绍
OrderedDict():实现字典的固定排序,是字典的子类
import collections dic = collections.OrderedDict() dic['k1'] = 3 dic['k2'] = 'nihao' dic['k3'] = 7 print(dic)
结果: OrderedDict([('k1', 3), ('k2', 'nihao'), ('k3', 7)])
x.move_to_end():把一个元素移动到最后
import collections dic = collections.OrderedDict() dic['k1'] = 3 dic['k2'] = 'nihao' dic['k3'] = 7 print(dic) dic.move_to_end('k2') print(dic)
结果: OrderedDict([('k1', 3), ('k2', 'nihao'), ('k3', 7)]) OrderedDict([('k1', 3), ('k3', 7), ('k2', 'nihao')])
x.popitem():移除最后一个写入字典的值,并可返回获取
x.pop():移除指定值并可返回获取