Why dictionary values aren't in the inserted order?

前端 未结 6 869
野的像风
野的像风 2020-12-19 02:37

When i declare a list 1,2,3,4 and i do something with it , even just print i get back the same sequence 1,2,3,4.

But when i do anything with dictionaries , they alw

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 02:59

    dictionary sort order is undefined! Do not rely on it for anything. Look for a sorted dictionary if you really want a sorted dictionary, but usually you don't need one.

    Examples:

    • python 2.7, it's built in to the collections module
    • Django has a SortedDict shipped with it
    • 2.4-2.7 you can use the ordereddict module, you can pip install or easy_install it

提交回复
热议问题