- 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:

- Java應用與實戰
- PyTorch Artificial Intelligence Fundamentals
- 新編Premiere Pro CC從入門到精通
- TypeScript圖形渲染實戰:基于WebGL的3D架構與實現
- Building Cross-Platform Desktop Applications with Electron
- Serverless架構
- Python機器學習:預測分析核心算法
- Java程序員面試筆試寶典(第2版)
- Python從入門到精通
- Python:Deeper Insights into Machine Learning
- Cocos2d-x Game Development Blueprints
- Java 從入門到項目實踐(超值版)
- 分布式架構原理與實踐
- Getting Started with React VR
- Python預測分析實戰