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

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:

主站蜘蛛池模板: 乐亭县| 新兴县| 阿拉善左旗| 来凤县| 章丘市| 紫金县| 黔江区| 宜州市| 南漳县| 宿松县| 合水县| 深圳市| 洛宁县| 北安市| 广河县| 延长县| 常德市| 扬州市| 称多县| 望奎县| 秭归县| 贡嘎县| 玉山县| 恭城| 马山县| 柳州市| 广东省| 石城县| 得荣县| 广宗县| 兴业县| 张家川| 余江县| 无为县| 玉溪市| 抚松县| 沂南县| 富民县| 贵德县| 靖远县| 墨玉县|