Re: [Python-ideas] About adding a new iterator methodcalled "shuffled"

Note that using sorting to shuffle is likely very inefficient. The sort takes O(n lg n) comparisons whereas you can do a perfect Fischer-Yates (aka Knuth) shuffle with <= n swaps. The model of computation here is different (comparisons vs swaps), but there is a vast literature on number of swaps done by sorting algorithms. In any case there's almost certainly no reason to use anything other than the standard Knuth shuffle, which is presumably what random.shuffle implements. Terry

Note that using sorting to shuffle is likely very inefficient.
Who cares? The OP's goal was to save a few programmer clock cycles so he could in-line what we already get from random.shuffle(). His request is use case challenged (very few programs would benefit and those would only save a line or two). If he actually cares about O(n) time then it's trivial to write: s = list(iterable) random.shuffle(s) for elem in s: . . . But if he want's to mush it on one-line, I gave a workable alternative. Raymond

Note that using sorting to shuffle is likely very inefficient.
Who cares? The OP's goal was to save a few programmer clock cycles so he could in-line what we already get from random.shuffle(). His request is use case challenged (very few programs would benefit and those would only save a line or two). If he actually cares about O(n) time then it's trivial to write: s = list(iterable) random.shuffle(s) for elem in s: . . . But if he want's to mush it on one-line, I gave a workable alternative. Raymond
participants (3)
-
Jacob Holm
-
Raymond Hettinger
-
Terry Jones