Python 的无序设置可以被视为随机顺序吗?

发布于 2024-09-01 21:09:04 字数 130 浏览 2 评论 0原文

我想知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晨与橙与城 2024-09-08 21:09:04

不,它不是随机的。它是“任意排序的”,这意味着您不能依赖它是有序的还是随机的。

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

被翻牌 2024-09-08 21:09:04

一句话,不:

>>> list(set(range(10000))) == list(range(10000))
True

In a word, no:

>>> list(set(range(10000))) == list(range(10000))
True
↘紸啶 2024-09-08 21:09:04

只是关于订单严格性的说明。看起来即使在相同的运行环境下也很不可靠。

例如,此代码给出了不同的答案:

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

在此处输入图像描述

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

携余温的黄昏 2024-09-08 21:09:04

不,您不能依赖它来实现任何真正的统计目的。 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.

电影里的梦 2024-09-08 21:09:04

任意性是设计程序时的核心,您保留的每一个自由就像一张小丑卡,您可以在实现、开发或重写程序时使用它。您收集的免费卡越多,您的代码的效率就越高(可能),因为您有更多的自由来更改它。

这不是随机的,这只是自由。如果这样设置更好,则订单可以在周三向前移动,在周五“向后”移动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文