Python 的无序设置可以被视为随机顺序吗?
我想知道Python内置set
结构缺乏元素排序是否“足够随机”。例如,采用集合的迭代器,是否可以将其视为其元素的打乱视图?
(如果重要的话,我在 Windows 主机上运行 Python 2.6.5。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,它不是随机的。它是“任意排序的”,这意味着您不能依赖它是有序的还是随机的。
No, it is not random. It is "arbitrarily ordered", which means that you cannot depend on it being either ordered or random.
一句话,不:
In a word, no:
只是关于订单严格性的说明。看起来即使在相同的运行环境下也很不可靠。
例如,此代码给出了不同的答案:
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:
不,您不能依赖它来实现任何真正的统计目的。 Python 中集合的实现是根据哈希表进行的,并且可以导致元素分布显示一些非常非随机的属性。 “不保证顺序”和“保证以统一随机的方式无序”之间存在很大差距。
使用
random.shuffle
来真正打乱序列中的元素。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.任意性是设计程序时的核心,您保留的每一个自由就像一张小丑卡,您可以在实现、开发或重写程序时使用它。您收集的免费卡越多,您的代码的效率就越高(可能),因为您有更多的自由来更改它。
这不是随机的,这只是自由。如果这样设置更好,则订单可以在周三向前移动,在周五“向后”移动。
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.