Can Python's set absence of ordering be considered random order?

前端 未结 5 2321
鱼传尺愫
鱼传尺愫 2020-12-06 01:30

I\'d like to know if the absence of element ordering of the Python\'s built-in set structure is \"random enough\". For instance, taking the iterator of a set, c

相关标签:
5条回答
  • 2020-12-06 01:40

    Just a note about the rigorously of the order. It seems that it is very unreliable even in the same running environment.

    For example this code gives different answers:

    data = 'KSRNDOW3GQ'
    chars = set(data)
    print(list(chars))
    

    0 讨论(0)
  • 2020-12-06 01:44

    In a word, no:

    >>> list(set(range(10000))) == list(range(10000))
    True
    
    0 讨论(0)
  • 2020-12-06 02:00

    No, it is not random. It is "arbitrarily ordered", which means that you cannot depend on it being either ordered or random.

    0 讨论(0)
  • 2020-12-06 02:00

    No, you can not rely on it for any real statistical purpose. The implementation of sets in Python is in terms of a hash table, and can cause the element distribution to display some very non-random properties. There's a large gap between "not having a guaranteed order" and "guaranteed to be unordered in a uniform-random manner".

    Use random.shuffle to really shuffle elements of a sequence.

    0 讨论(0)
  • 2020-12-06 02:01

    Arbitrariness is central when designing programs, each of these freedoms that you reserve is like a joker card that you can use when you implement, develop, or rewrite your program. The more of these free-cards you collect, the more efficiency can you deliver from your code (probably), since you have more freedom to change it.

    It is not random, it's only freedom. If it's a better set that way, the order can be forwards on Wednesdays and "backwards" on Fridays.

    0 讨论(0)
提交回复
热议问题