OrderedDict 是 Python 中 Dict 对象的子类。字典和ordereddct的区别在于 ordereddct 本身保持插入的键的顺序,而在字典中,键的顺序不是重要的部分。
OrderedDict 是标准的库类。它位于collections模块中。
为了使用它,用户必须导入 collections 标准库模块。
示例:
import collections
在本文中,我们将讨论 ordereddct 上的一些操作,以及 Dict 与 ordereddct 有何不同。
用户可以在 Dict 类和 OrderedDict 类中放入一些键和很多带有值的键。在下面的例子中,我们将展示 Dict 类的顺序是如何变化的,但是对于 OrderedDict 类,它将保持不变。
示例:
import collections
# we will first create normal dict
print('Dict:')
user_dict = {}
user_dict['PP'] = 10
user_dict['QQ'] = 20
user_dict['RR'] = 30
user_dict['SS'] = 40
user_dict['TT'] = 50
user_dict['UU'] = 60
for item in user_dict.items():
print(item)
print()
# now, we will create ordered dict
print('OrderedDict:')
user_ordered_dict = collections.OrderedDict()
user_ordered_dict['PP'] = 10
user_ordered_dict['QQ'] = 20
user_ordered_dict['RR'] = 30
user_ordered_dict['SS'] = 40
user_ordered_dict['TT'] = 50
user_ordered_dict['UU'] = 60
for item in user_ordered_dict.items():
print(item)
输出:
Dict:
('PP', 10)
('QQ', 20)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
OrderedDict:
('PP', 10)
('QQ', 20)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
更改特定键的值
对于ordereddct类,改变特定键的值后,键的顺序不会改变,但是在 Dict 类中,顺序可以改变,也可以不改变。
示例:
import collections
# we will first create normal dict
print('Dict:')
user_dict = {}
user_dict['PP'] = 10
user_dict['QQ'] = 20
user_dict['RR'] = 30
user_dict['SS'] = 40
user_dict['TT'] = 50
user_dict['UU'] = 60
for item in user_dict.items():
print(item)
#now, we will change the value for key QQ
user_dict['QQ'] = 111
print('After changing value of specific key in Dict')
for item in user_dict.items():
print(item)
print()
#we will create ordered dict
print('OrderedDict:')
user_ordered_dict = collections.OrderedDict()
user_ordered_dict['PP'] = 10
user_ordered_dict['QQ'] = 20
user_ordered_dict['RR'] = 30
user_ordered_dict['SS'] = 40
user_ordered_dict['TT'] = 50
user_ordered_dict['UU'] = 60
for item in user_ordered_dict.items():
print(item)
# now, we will change the value for specific key QQ
user_ordered_dict['QQ'] = 111
print('After changing value of specific key in Ordered Dict')
for item in user_ordered_dict.items():
print(item)
输出:
Dict:
('PP', 10)
('QQ', 20)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
After changing value of specific key in Dict
('PP', 10)
('QQ', 111)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
OrderedDict:
('PP', 10)
('QQ', 20)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
After changing value of specific key in Ordered Dict
('PP', 10)
('QQ', 111)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
删除和重新插入 OrderedDict 类中的元素
当我们从 OrderedDict 类中删除一个元素,然后执行该特定键和值的重新插入操作时,它会将该元素推到后面。虽然,OrderedDict 类在插入过程中保持顺序,但是当执行删除过程时,它会移除顺序信息,并将重新插入的元素视为新条目。
示例:
import collections
#we will create ordered dict
print('OrderedDict:')
user_ordered_dict = collections.OrderedDict()
user_ordered_dict['PP'] = 10
user_ordered_dict['QQ'] = 20
user_ordered_dict['RR'] = 30
user_ordered_dict['SS'] = 40
user_ordered_dict['TT'] = 50
user_ordered_dict['UU'] = 60
for item in user_ordered_dict.items():
print(item)
#First we will delete item in key RR
user_ordered_dict.pop('RR')
print('After Deleting the key')
for item in user_ordered_dict.items():
print(item)
#now, we will re-inserte the item
user_ordered_dict['RR'] = 30
print('After Re-inserting the key and value')
for item in user_ordered_dict.items():
print(item)
输出:
OrderedDict:
('PP', 10)
('QQ', 20)
('RR', 30)
('SS', 40)
('TT', 50)
('UU', 60)
After Deleting the key
('PP', 10)
('QQ', 20)
('SS', 40)
('TT', 50)
('UU', 60)
After Re-inserting the key and value
('PP', 10)
('QQ', 20)
('SS', 40)
('TT', 50)
('UU', 60)
('RR', 30)
如何在订单的开头插入订单
当用户想要在 OrderedDict 类的开头插入一些元素时,他/她可以使用“更新”方法。
示例:
from collections import OrderedDict
user_ordered_dict = OrderedDict([('Jake', '10'), ('John', '20'), ('Ross', '40')])
print("The current dictionary values are :")
print(user_ordered_dict)
user_ordered_dict.update({'Ryan':'70'})
user_ordered_dict.move_to_end('Ryan', last = False)
print("The updated dictionary values are : ")
print(user_ordered_dict)
在上面的代码中:
- 首先,我们已经导入了所需的包。
- 然后,我们通过使用 order ordinate 创建了有序字典。
- 我们使用“更新”方法来指定键及其值。
- 然后,我们使用’ move_to_end ‘函数将键及其值移动到最后。
- 显示所需的输出。
输出:
The current dictionary values are :
OrderedDict([('Jake', '10'), ('John', '20'), ('Ross', '40')])
The updated dictionary values are :
OrderedDict([('Ryan', '70'), ('Jake', '10'), ('John', '20'), ('Ross', '40')])
如何用 OrderedDict 检查字符串中的字符顺序
如果用户想要检查字符串中字符的顺序,他们可以使用“OrderedDict”方法。让我们理解下面的例子。
示例:
from collections import OrderedDict
def check_order(user_input, user_pattern):
user_dict = OrderedDict.fromkeys(user_input)
pattern_length = 0
for key,value in user_dict.items():
if (key == user_pattern[pattern_length]):
pattern_length = pattern_length + 1
if (pattern_length == (len(user_pattern))):
return 'The order of pattern is correct'
return 'The order of pattern is incorrect'
user_input = 'Hello Jake'
input_pattern = 'Ja'
print("The string is ")
print(user_input)
print("The input pattern is ")
print(input_pattern)
print(check_order(user_input,input_pattern))
user_input = 'Hello Jake'
input_pattern = 'ke'
print("The string is ")
print(user_input)
print("The input pattern is ")
print(input_pattern)
print(check_order(user_input,input_pattern))
在上面的代码中:
- 首先,我们导入了所需的包。
- 然后,我们定义了“check_order”方法,该方法将采用两个参数。
- 然后,我们使用“fromkeys”方法创建了有序字典。
- 我们已经将模式的长度初始化为 0。
- 如果键等于模式,则模式的长度将增加。
- 但是,如果模式的长度与当前长度相同,这意味着顺序是正确的。否则,订单不正确。
输出:
The string is
Hello Jake
The input pattern is
Ja
The order of pattern is correct
The string is
Hello Jake
The input pattern is
ke
The order of pattern is incorrect
结论
在本教程中,我们讨论了 OrderedDict 以及它与普通字典的区别。我们还解释了用户如何在 OrderedDict 中删除和插入元素,并在字典顺序的开头重新排列它们。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/246724.html
微信扫一扫
支付宝扫一扫