Can Python's set absence of ordering be consid

2019-01-15 13:25发布

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, can it be considered a shuffled view of its elements?

(If it matters, I'm running Python 2.6.5 on a Windows host.)

5条回答
乱世女痞
2楼-- · 2019-01-15 13:35

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))

enter image description here

查看更多
The star\"
3楼-- · 2019-01-15 13:36

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.

查看更多
狗以群分
4楼-- · 2019-01-15 13:42

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

查看更多
聊天终结者
5楼-- · 2019-01-15 13:44

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.

查看更多
Bombasti
6楼-- · 2019-01-15 13:53

In a word, no:

>>> list(set(range(10000))) == list(range(10000))
True
查看更多
登录 后发表回答