- Python Data Structures and Algorithms
- Benjamin Baka
- 171字
- 2021-07-09 19:45:05
Ordered dictionaries
The important thing about ordered dictionaries is that they remember the insertion order, so when we iterate over them, they return values in the order they were inserted. This is in contrast to a normal dictionary, where the order is arbitrary. When we test to see whether two dictionaries are equal, this equality is only based on their keys and values; however, with an OrderedDict, the insertion order is also considered An equality test between two OrderedDicts with the same keys and values but a different insertion order will return False:

Similarly, when we add values from a list using update, the OrderedDict will retain the same order as the list. This is the order that is returned when we iterate the values, for example:

The OrderedDict is often used in conjunction with the sorted method to create a sorted dictionary. For example, in the following example we use a lambda function to sort on the values, here we use a numerical expression to sort the integer values:

- Spring 5.0 By Example
- Java Web基礎與實例教程(第2版·微課版)
- Learning RabbitMQ
- 軟件測試工程師面試秘籍
- 軟件架構:Python語言實現
- VMware虛擬化技術
- Hands-On Natural Language Processing with Python
- Learning R for Geospatial Analysis
- Creating Stunning Dashboards with QlikView
- RESTful Java Web Services(Second Edition)
- Unity 2018 Augmented Reality Projects
- Getting Started with Polymer
- Mastering Adobe Captivate 7
- 深入解析Java編譯器:源碼剖析與實例詳解
- Vue.js光速入門及企業項目開發實戰