1. 程式人生 > >Python_collections_OrderedDict有序字典部分功能介紹

Python_collections_OrderedDict有序字典部分功能介紹

固定排序 元素 red () 子類 port import color style

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():移除指定值並可返回獲取

Python_collections_OrderedDict有序字典部分功能介紹