问题描述
我有一个关于 .pop() 方法的问题.在文档中,它指出在集合上:
I have a question regarding the .pop() method. In the documentation, it states that on sets:
从集合中移除并返回任意元素.
那么任意"究竟是什么意思?例如:如果我想从随机确定位置的用户列表中构建一个队列,是否可以将所有用户输入到一个集合中,然后使用 .pop() 构建一个随机分配的队列?
So what exactly does "arbitrary" mean? Like, for example: If I wanted to build a queue from a list of users where position is determined at random, could I enter in all users into a set, and then build a randomly assigned queue using .pop()?
所以代码看起来像
queue_set = {'bob','rachel','sara','david'}
queue = dequeue([])
while queue_set:
queue.append(queue_set.pop())
这是将成员随机分配到索引的合理方法吗?还是 .pop() 依赖于某种输入数据的方式?
Would that be a reasonable way to randomly assign members to an index? Or is the .pop() reliant on some way the data is entered or something?
谢谢!
推荐答案
没有
任意"意味着实现返回最方便的元素.你不能保证随机性.如果需要随机选择,请使用 random.sample()
或 random.shuffle()
.
"Arbitrary" means the implementation returns whichever element is most convenient. You are not guaranteed randomness. Use random.sample()
or random.shuffle()
if you need random selection.
这篇关于Python:无序集合上的 .pop()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!