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

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:

主站蜘蛛池模板: 增城市| 巴塘县| 衡阳市| 弋阳县| 自治县| 兴海县| 阿拉善左旗| 双城市| 徐州市| 普洱| 新化县| 西昌市| 乐安县| 蛟河市| 湟源县| 隆德县| 龙山县| 喀什市| 济源市| 隆林| 遂川县| 呼玛县| 四川省| 长子县| 赞皇县| 寻乌县| 炎陵县| 丁青县| 伊宁市| 静海县| 河东区| 昌吉市| 连州市| 利川市| 个旧市| 佛冈县| 北安市| 牙克石市| 沙河市| 阿城市| 庆阳市|