[英]Order of elements returned by set
So I was converting a list with repetitive elements into a set to have each element appear once.所以我将一个包含重复元素的列表转换为一个集合,让每个元素出现一次。 I know that sets are unordered so they will display the elements in the order given.我知道集合是无序的,所以它们会按照给定的顺序显示元素。 I ran the below script and I noticed a strange output.我运行了下面的脚本,我注意到一个奇怪的输出。
mylist = [1,2,2,33,4,4,11,22,3,3,2]
print(set(mylist))
The output would be:输出将是:
{1, 2, 33, 4, 3, 11, 22}
The 3 in the original list appears after the 11 and 22, so why does it appear before them in the set?原列表中的 3 出现在 11 和 22 之后,那么为什么它在集合中出现在它们之前呢?
Sets in Python do not have an order Python 中的集合没有顺序
A set object is an unordered collection of distinct hashable objects.集合对象是不同的可散列对象的无序集合。 Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.常见用途包括成员资格测试、从序列中删除重复项以及计算交集、并集、差和对称差等数学运算。
https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.