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

- 數據庫系統教程(第2版)
- PyTorch自動駕駛視覺感知算法實戰
- C++ Builder 6.0下OpenGL編程技術
- Web交互界面設計與制作(微課版)
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- JavaScript入門經典
- Working with Odoo
- RISC-V體系結構編程與實踐(第2版)
- PLC應用技術(三菱FX2N系列)
- Solr Cookbook(Third Edition)
- Extreme C
- 零基礎學Scratch 3.0編程
- Instant Apache Camel Messaging System
- Mobile Forensics:Advanced Investigative Strategies
- Tkinter GUI Application Development Blueprints