Skip to main content
 首页 » 编程设计

python设置元素序列

2025年05月04日14txw1958

<分区>

>>> x = {"a","b","c","d","e"} 
>>> print x 
set(['a', 'c', 'b', 'e', 'd']) 

谁能帮忙解释一下为什么set元素打印出来后顺序变了?

-- 更新: 谢谢大家!

是的,set是一组元素,“set”没有顺序。 但是每次打印,输​​出都是一样的。 请帮忙解释为什么集合 {"a","b","c","d","e"} 被保存为 ['a', 'c', 'b', 'e', 'd' ] 由 python,以什么样的顺序?好像不是ASCII字符的顺序。

-- 更新: Paul Rooney 的回答是我想知道的根本原因。集合输出的顺序与集合中每个元素的哈希有关,官方文档对此有说明。谢谢!

The order is dictated by the hashes of the values, the size of the underlying hash table and the number of hash collisions that occurred. See here --Paul Rooney