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

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:

主站蜘蛛池模板: 醴陵市| 海口市| 什邡市| 富裕县| 东明县| 安岳县| 萨嘎县| 湟源县| 芒康县| 年辖:市辖区| 尖扎县| 轮台县| 洛川县| 惠州市| 浦江县| 长子县| 德安县| 台中县| 灌阳县| 延寿县| 惠水县| 河曲县| 达州市| 牙克石市| 谢通门县| 保定市| 台安县| 娄底市| 惠水县| 拉萨市| 安平县| 亚东县| 札达县| 阳朔县| 凭祥市| 绥宁县| 寻乌县| 建宁县| 乐清市| 定远县| 徐水县|