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

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:

主站蜘蛛池模板: 克山县| 临夏市| 延安市| 闸北区| 和龙市| 咸宁市| 新沂市| 临洮县| 洞口县| 沐川县| 五原县| 九龙坡区| 西城区| 顺平县| 嘉义县| 玉龙| 宁夏| 伊宁市| 松溪县| 彭州市| 长乐市| 达孜县| 翁源县| 门头沟区| 枣庄市| 镇赉县| 兴城市| 通渭县| 靖宇县| 衢州市| 嵊泗县| 安阳县| 湖北省| 饶平县| 安达市| 新泰市| 枞阳县| 昭通市| 江西省| 临邑县| 潜江市|