OrderedDict()
时间: 2024-05-12 10:16:36 浏览: 11
OrderedDict() is a class in the collections module of Python that is used to create dictionaries that maintain the order of insertion of key-value pairs. This means that the elements of the dictionary are kept in the same order in which they were added.
The OrderedDict() class is similar to the regular dict() class, but it has some additional methods and properties that are useful for working with ordered dictionaries. For example, the OrderedDict() class has a method called move_to_end() that can be used to move a key to either end of the dictionary.
Here is an example of how to create an OrderedDict() in Python:
```
from collections import OrderedDict
my_dict = OrderedDict()
my_dict['a'] = 1
my_dict['b'] = 2
my_dict['c'] = 3
print(my_dict)
```
Output:
```
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
```
As you can see, the order of the elements in the dictionary is maintained.