官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 句容市| 景德镇市| 南安市| 英德市| 通榆县| 兴海县| 蕉岭县| 禹州市| 托克逊县| 信丰县| 行唐县| 九寨沟县| 嘉荫县| 浏阳市| 塔河县| 临潭县| 富平县| 宜阳县| 福贡县| 额济纳旗| 陆丰市| 广东省| 桓仁| 明水县| 休宁县| 南岸区| 保亭| 竹北市| 拜泉县| 昌图县| 甘南县| 佛山市| 临沭县| 旌德县| 阜南县| 磐石市| 新巴尔虎右旗| 华容县| 卢氏县| 孟连| 镇原县|