为什么多次运行程序时python set对象中元素的顺序会改变

I have this set and when I run this program twice the order of printing is changed

S = {7, "veil", 0, -29, ("x", 11), "sun", frozenset({8, 4, 7}), 913}
print(S)

first time output:- {'veil', 0, -29, 'sun', 7, frozenset({8, 4, 7}), ('x', 11), 913}

second time output:- {0, -29, 7, frozenset({8, 4, 7}), 'veil', 913, ('x', 11), 'sun'}

I assumed that this behaviour is because only hashable objects can be added to a set and it is unordered but since a dictionary's keys are hashable and it is unordered then a dictionary should show this behaviour too, but it doesn't

Why does the order of the elements change in the set?

评论
  • DJHongKong
    DJHongKong 回复

    正如奥利弗(Oliver)所说,集合是项目的无序列表,因此次序是随机的。