[ python-Bugs-936988 ] Random.choice doesn't work with sets.

SourceForge.net noreply at sourceforge.net
Tue Apr 20 15:52:21 EDT 2004


Bugs item #936988, was opened at 2004-04-17 11:20
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=936988&group_id=5470

Category: Python Library
Group: Python 2.3
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Jeremy Fincher (jemfinch)
Assigned to: Nobody/Anonymous (nobody)
Summary: Random.choice doesn't work with sets.

Initial Comment:
Random.sample was changed to work with sets and other non-
indexable iterables, but random.choice still requires that its 
argument be a full sequence.

Should that possibly be changed, so that for lists random.choice 
uses the current implementation, but for other iterables that 
random.sample supports, it could return random.sample(iterable, 
1)[0]?

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-04-20 14:52

Message:
Logged In: YES 
user_id=80475

I would not like to see that accommodation extended to
random.choice().

In random.sample(), it was done to eliminate a surprising
assymetry  between the two internal algorithms.  Because one
used list(population) and the other used
population.__getitem__(), the first would work with any
iterable and the second demanded random access.  So, for
certain values of n, dictionaries would work find and for
other values they would  fail.  

This consideration does not apply to random.choice().  I
think most people have a mental model of how it works and
would be surprised to have an iterable input fully
manitested in memory.  Though, I do grant that it is also
surprising that it does not work with sets.  C'est le vie.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=936988&group_id=5470



More information about the Python-bugs-list mailing list