gpt4 book ai didi

python - 什么决定了 Python 字典中数据的顺序?

转载 作者:行者123 更新时间:2023-11-28 17:46:58 27 4
gpt4 key购买 nike

什么决定了字典中项目的顺序(特别是在 Python 中,尽管这可能适用于其他语言)?例如:

>>> spam = {'what':4, 'shibby':'cream', 'party':'rock'}
>>> spam
{'party': 'rock', 'what': 4, 'shibby': 'cream'}

如果我再次调用垃圾邮件,这些项目仍将按相同的顺序排列。但是这个顺序是怎么决定的呢?

最佳答案

根据python docs ,

Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.

它们是任意的,同样来自文档:

A dictionary’s keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 1 and 1.0) then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.)

关于python - 什么决定了 Python 字典中数据的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16724204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com