[ python-Bugs-936988 ] Random.choice doesn't work with sets.
SourceForge.net
noreply at sourceforge.net
Tue Apr 20 21:39:51 EDT 2004
Bugs item #936988, was opened at 2004-04-17 12:20
Message generated for change (Comment added) made by tim_one
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: Tim Peters (tim_one)
Date: 2004-04-20 21:39
Message:
Logged In: YES
user_id=31435
I agree with Raymond on this -- making random.choice() an O
(n) function in some cases would make it an attractive
nuisance. In the absence of an efficient implementation, it's
regrettable that random.sample() decided to cater to sets.
Doing more damage of the same kind would primarily be doing
more damage.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-04-20 21:23
Message:
Logged In: YES
user_id=80475
Sorry, I don't think this is a good idea and do not want to
introduce
O(n) behavior into an otherwise O(1) function.
----------------------------------------------------------------------
Comment By: Jeremy Fincher (jemfinch)
Date: 2004-04-20 19:26
Message:
Logged In: YES
user_id=99508
An iterable input does not have to be fully manifested in memory for
random.choice to work.
See the implementation at <http://www.pastebin.de/pastebin.py?
id=594>.
Is this to say that random.sample entirely manifests large iterables in
memory? I would say that this would be just as unexpected as
random.choice doing so. If it's not the case, then why can't
random.choice do what it does now for proper sequences, but just
dispatch to random.sample(iterable, 1)[0] in all other cases?
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-04-20 15: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