1

当一个集合中的全部类型为数值时,pop()将升序输出,这一点不明白,集合不是无序的吗?

translation:

When all the types in a collection are numeric, pop() will output in ascending order.I don't know why, isn't the set unordered?

s = {2,5,3,7,0} for i in range(5): print(s.pop())

output: 0 2 3 5 7

WangTanxu
  • 71
  • 6
  • I have found a similar question in this topic, it could help you https://stackoverflow.com/questions/1867861/dictionaries-how-to-keep-keys-values-in-same-order-as-declared – Baptiste Gavalda Nov 14 '18 at 14:22
  • 1
    See https://stackoverflow.com/questions/21017188/set-pop-isnt-random – B. M. Nov 14 '18 at 14:34

1 Answers1

0

It doesn't occur if the numbers are higher

>>> l=list(range(655360, 6553600))
>>> for _ in range(20):
...  el = ra.choice(l)
...  s.add(el)
...  print(el)
... 
3589721
2762448
1543072
4489208
5381194
4181645
1470069
6256052
5833067
6288785
3707357
1414093
3347945
1358111
1754489
4503096
3424305
2233038
4817376
3941735
>>> for _ in range(10):
...  print(s.pop())
... 
4181645
6288785
1358111
1543072
3424305
6256052
4503096
5381194
1414093
2233038

It's a Python implementation detail and you should not rely on it.

As recommended in the comments, please see this answer

Pynchia
  • 10,996
  • 5
  • 34
  • 43