[issue7522] random.choice should accept a set as input

Raymond Hettinger report at bugs.python.org
Wed Dec 16 18:57:27 CET 2009


Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:

The underlying data structure for sets doesn't lend itself to an
efficient method of random selection.  It is best for the programmer to
explictly convert to a sequence and then make the random selection (that
way the conversion cost isn't hidden). 

If you don't mind the inefficiency of an implicit conversion to a list,
you can use "random.sample(s, 1)[0]".  If only an arbitrary element is
needed, use "x=next(iter(s))" to non-destructively fetch the first element.

----------
resolution:  -> rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7522>
_______________________________________


More information about the Python-bugs-list mailing list