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

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:

主站蜘蛛池模板: 瓮安县| 西丰县| 镇远县| 洪湖市| 泰安市| 响水县| 班戈县| 赣州市| 林口县| 乌恰县| 弋阳县| 两当县| 寿光市| 巴塘县| 静宁县| 胶南市| 巴塘县| 时尚| 泉州市| 三门峡市| 古交市| 夹江县| 金平| 瑞金市| 灌云县| 舒城县| 张北县| 上栗县| 屏南县| 博白县| 乌兰县| 黄大仙区| 洞口县| 谢通门县| 泰顺县| 潞西市| 丹巴县| 西林县| 榆树市| 斗六市| 永春县|