从集合中选择随机项目 [英] selecting a random item from a set

查看:67
本文介绍了从集合中选择随机项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当多的algortihms突出显示一次从一组中选择/删除一个随机的

元素。乍一看,我想不出更好的东西

用`sets.Set`实现这个目的而不是:


for dummy ,zip中的randomElement(范围(randrange(len(s)+1)),s):传递

#可能后跟

s.remove(randomElement)


还有更好的方法吗?如果没有,像Set.randompop()这样的东西怎么样?


''作为

解决方案

亚历山大Schmolck写道:

有更好的方法吗?如果没有,像Set.randompop()这样的东西怎么样?




原则上,random.choice应该工作。它没有,因为它期望

这个集合是可索引的。


我不希望Set类型增长随机方法: - )


问候,

马丁


Alexander Schmolck< a。*** *****@gmx.net>写道:

相当多的algortihms突出显示一次从一组中选择/删除
单个随机元素。乍一看,我无法想象用sets.Set来实现这一点,而不是像以下那样:

用于虚拟,randomElement in zip (范围(randrange(len(s)+1)),s):传递
#可能后跟
s.remove(randomElement)

有没有更好的方法?如果没有,像Set.randompop()这样的东西怎么样?




这样做的经典方法是这样的:


表示n,randomElement in enumerate(s):

if random()< (1.0 /(n + 1)):

e = randomElement

#可能后跟

s.remove(randomElement)


请注意,您无需事先知道套装的大小。你可以使用相同的方法(例如)从文件中选择一个随机行,

而不知道文件有多少行,而且不需要

读取文件两次或将其存储在内存中。


看看为什么这样做(除非我犯了一个错误)留作练习:)。


Paul Rubin< http://ph****@NOSPAM.invalid>写道:

这样做的经典方法如下:

n,枚举中的randomElement:
如果random()< (1.0 /(n + 1)):
e = randomElement
#可能后跟
s.remove(randomElement)




哎呀,意思是说:


代表n,e代表枚举:

if random()< (1.0 /(n + 1)):

randomElement = e

#可能后跟

s.remove(randomElement)


Quite a few algortihms prominently feature choosing/removing a single random
element from a set at a time. On first sight I can''t think of anything better
to achieve this with `sets.Set` than something along the lines of:

for dummy, randomElement in zip(range(randrange(len(s)+1)), s): pass
# possibly followed by
s.remove(randomElement)

Is there a better way? If not, how about something like Set.randompop()?

''as

解决方案

Alexander Schmolck wrote:

Is there a better way? If not, how about something like Set.randompop()?



In principle, random.choice "ought to" work. It doesn''t, as it expects
the set to be indexable.

I would not like the Set type to grow random methods :-)

Regards,
Martin


Alexander Schmolck <a.********@gmx.net> writes:

Quite a few algortihms prominently feature choosing/removing a
single random element from a set at a time. On first sight I can''t
think of anything better to achieve this with `sets.Set` than
something along the lines of:

for dummy, randomElement in zip(range(randrange(len(s)+1)), s): pass
# possibly followed by
s.remove(randomElement)

Is there a better way? If not, how about something like Set.randompop()?



The classic way to do it goes something like this:

for n, randomElement in enumerate(s):
if random() < (1.0 / (n+1)):
e = randomElement
# possibly followed by
s.remove(randomElement)

Note that you don''t need to know the size of the set in advance. You
can use the same method for (e.g.) choosing a random line from a file,
without knowing how many lines the file has, and without having to
read the file twice or store it in memory.

Seeing why this works (unless I made an error) is left as an exercise :).


Paul Rubin <http://ph****@NOSPAM.invalid> writes:

The classic way to do it goes something like this:

for n, randomElement in enumerate(s):
if random() < (1.0 / (n+1)):
e = randomElement
# possibly followed by
s.remove(randomElement)



Oops, meant to say:

for n, e in enumerate(s):
if random() < (1.0 / (n+1)):
randomElement = e
# possibly followed by
s.remove(randomElement)


这篇关于从集合中选择随机项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆