Items ordering in Python dictionary

前端 未结 2 541
一整个雨季
一整个雨季 2020-12-11 19:32

I am in simple doubt... I created the following dictionary:

>>> alpha={\'a\': 10, \'b\': 5, \'c\': 11}

But, when I want to see the

相关标签:
2条回答
  • 2020-12-11 19:59

    Dictonaries are not guaranting sorting of keys. You can find this information in python docs: http://docs.python.org/tutorial/datastructures.html#dictionaries

    You can always sort dictionary keys or use other, more specialized collection.

    0 讨论(0)
  • 2020-12-11 20:02

    Dictionaries are unordered containers - if you want to preserve order, you can use collections.OrderedDict (Python 2.7 or later), or use another container type which is naturally order-preserving.

    Generally if you have an access pattern that cares about ordered retrieval then a dictionary is solving a problem you don't have (fast access to random elements), while giving you a new one.

    0 讨论(0)
提交回复
热议问题