[Python-ideas] [Python-Dev] About adding a new iterator methodcalled "shuffled"
Mark Dickinson
dickinsm at gmail.com
Tue Mar 24 19:44:51 CET 2009
On Tue, Mar 24, 2009 at 6:29 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On the other hand, sorting is O(n.log(n)), which is probably sub-optimal for
> shuffling (I don't know how shuffle() is internally implemented, but ISTM that
> it shouldn't take more than O(n)).
I assumed that the OP was suggesting something of the form:
def shuffled(L):
while L:
i = random.randrange(len(L))
yield L[i]
L.pop(i)
fixed up somehow so that it's only O(1) to yield each element; in effect,
an itertools version of random.sample. I could see uses for this
in cases where you only want a few randomly chosen elements
from a large list, but don't necessarily know in advance how many
elements you need.
Mark
More information about the Python-ideas
mailing list