python-3 中设置的转换列表不一致

Inconsistency of converting list to set in python-3

我对 Python-3 如何以不一致的方式执行列表设置转换感到困惑。当输入列表按降序排序时,它会按升序创建一个集合。当输入列表未排序时,它什么也不做,结果集也未排序。在一种情况下,甚至未排序的列表也会转换为已排序的集合。

# list is unsorted and resulting set is unsorted 
lst1 = [9,100,1]
thisset1 = set(lst1)
print(thisset1)     # 9,100,1

# list is sorted in descending order and 
# resulting set is sorted in ascending order
lst2 = [4,3,2,1]
thisset2 = set(lst2)
print(thisset2)     # 1,2,3,4

# list is sorted in descending order and 
# resulting set is sorted in ascending order
lst4 = [500,400,200]
thisset4 = set(lst4)
print(thisset4)    # 500,400,200

# list is unsorted but resulting set is SORTED in ascending order 
lst3 = [4,1,3,2]
thisset3 = set(lst3)
print(thisset3)

是不是因为哈希?

集合是一种无序的数据结构。所以顺序是随机的。 举个你用过的例子。

lst4 = [500,400,200] 
set(lst4)  
{400, 500, 200}

这和你的不一样。因此相同输入的顺序可能不同。