OrderedDict是Python中的dict对象的子类。dictOrderedDict之间的区别在于,OrderedDict本身会维护按插入顺序排序的键,而在dict中,键的顺序不是重要的部分。

OrderedDict是Python标准库的一部分,位于collections模块中。

要使用它,用户必须导入collections标准库模块。

示例:

import collections  

在本文中,我们将讨论OrderedDict上的一些操作以及Dict与OrderedDict的区别。

用户可以将一些键和大量带有值的键放入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)

更改特定键的值

在更改特定键的值后,OrderedDict类的键的顺序不会改变,但在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的开头插入元素

当用户想要在OrderedDict类的开头插入一些元素时,可以使用'update'方法。

示例:

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)  

在上面的代码中:

  • 首先,我们导入了所需的包。
  • 然后,我们使用OrdereDict创建了Ordered Dictionary。
  • 我们使用'update'方法指定了键和其值。
  • 然后,我们使用'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_lengthpattern_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中的元素,并将它们重新排列到字典顺序的开头。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程