Python中的常规字典与有序字典
Python中的字典是数据值的无序集合,用于像地图一样存储数据值,与其他仅将单个值作为元素保存的数据类型不同,字典包含键:值对。字典中提供了键值,使其更加优化。常规字典类型不跟踪(键,值)对的插入顺序,因此根据它们在哈希表中的存储方式对键进行迭代,而哈希表又基于随机值,以减少冲突。
与此相反, Python提供了 OrderedDict 类型,它记住字典中 (key, value) 对的插入顺序,从而保留了顺序。 OrderedDict 比Python中的常规字典消耗更多内存,因为底层的双链表实现保留了顺序。
例子:
Python
# A Python program to demonstrate
# the difference between regular
# and ordered dictionary.
import collections
# Creating a regular dictionary
print('Regular dictionary:')
d = {chr(k):k for k in range(ord('a'), ord('g'))}
for k, v in d.items():
print(k, v)
# Creating an Ordered dictionary
print('\nOrderedDict:')
d = collections.OrderedDict()
[d.setdefault(chr(k), k) for k in range(ord('a'), ord('g'))]
for k, v in d.items():
print(k, v)
Python
# A Python program to demonstrate
# working of deletion and re-insertion in
# regular and OrderedDict
from collections import OrderedDict
print("Before deleting:\n")
d = {}
print("Regular dictionary:")
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4
for key, value in d.items():
print(key, value)
od = OrderedDict()
print("\nOrdered dictionary:")
od['a'] = 1
od['b'] = 2
od['c'] = 3
od['d'] = 4
for key, value in od.items():
print(key, value)
print("\nAfter deleting:\n")
print("Regular dictionary:")
d.pop('c')
for key, value in d.items():
print(key, value)
print("\nOrdered dictionary:")
od.pop('c')
for key, value in od.items():
print(key, value)
print("\nAfter re-inserting:\n")
print("Regular dictionary:")
d['c'] = 3
for key, value in d.items():
print(key, value)
print("\nOrdered dictionary:")
od['c'] = 3
for key, value in od.items():
print(key, value)
输出 :
Regular dictionary:
('a', 97)
('c', 99)
('b', 98)
('e', 101)
('d', 100)
('f', 102)
OrderedDict:
('a', 97)
('b', 98)
('c', 99)
('d', 100)
('e', 101)
('f', 102)
注意:从Python 3.7 开始,保证Python字典的插入顺序。
删除和重新插入:
删除并重新插入相同的密钥会将其推到后面,因为 OrderedDict 但会保持插入顺序。
例子:
Python
# A Python program to demonstrate
# working of deletion and re-insertion in
# regular and OrderedDict
from collections import OrderedDict
print("Before deleting:\n")
d = {}
print("Regular dictionary:")
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4
for key, value in d.items():
print(key, value)
od = OrderedDict()
print("\nOrdered dictionary:")
od['a'] = 1
od['b'] = 2
od['c'] = 3
od['d'] = 4
for key, value in od.items():
print(key, value)
print("\nAfter deleting:\n")
print("Regular dictionary:")
d.pop('c')
for key, value in d.items():
print(key, value)
print("\nOrdered dictionary:")
od.pop('c')
for key, value in od.items():
print(key, value)
print("\nAfter re-inserting:\n")
print("Regular dictionary:")
d['c'] = 3
for key, value in d.items():
print(key, value)
print("\nOrdered dictionary:")
od['c'] = 3
for key, value in od.items():
print(key, value)
输出:
Before deleting:
Regular dictionary:
('a', 1)
('c', 3)
('b', 2)
('d', 4)
Ordered dictionary:
('a', 1)
('b', 2)
('c', 3)
('d', 4)
After deleting:
Regular dictionary:
('a', 1)
('b', 2)
('d', 4)
Ordered dictionary:
('a', 1)
('b', 2)
('d', 4)
After re-inserting:
Regular dictionary:
('a', 1)
('c', 3)
('b', 2)
('d', 4)
Ordered dictionary:
('a', 1)
('b', 2)
('d', 4)
('c', 3)