selecting a random item from a set
Alexander Schmolck
a.schmolck at gmx.net
Tue Dec 30 21:40:17 EST 2003
"Martin v. Loewis" <martin at v.loewis.de> writes:
> 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.
Maybe not insensibly -- the fairly generic approach outlined above that will
work for any iterable with len is clearly not desirable as default (because of
its time complexity), and just using it as a fallback won't work either,
because AFACT there is no way for `choice` to find out whether its argument is
a sequence or not.
> I would not like the Set type to grow random methods :-)
Well, one ought to do :)
'as
More information about the Python-list
mailing list