24 Mar
2009
24 Mar
'09
6:44 p.m.
On Tue, Mar 24, 2009 at 6:29 PM, Antoine Pitrou <solipsis@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