On Mon, Aug 03, 2020 at 03:04:40AM -0000, raymond.hettin...@gmail.com wrote:
> Steven D'Aprano wrote:
> > > This is easily solved with a three-line helper:
> > def shuffled(iterable):
>  ...
> > I have implemented this probably a half a dozen times, and I expect 
> > others have too.
> 

> FWIW, we've already documented a clean way to do it, 
> https://docs.python.org/3/library/random.html#random.shuffle , "To 
> shuffle an immutable sequence and return a new shuffled list, use 
> sample(x, k=len(x)) instead."

Yes, I remember the last time I played poker with some friends, and the 
dealer handed me the deck of cards and asked me to take a sample of 52 
cards *wink*

While you are technically correct that a sample of N from a sequence of 
length N is equivalent to shuffling, that's not a particularly obvious 
thing to do, and the semantics of shuffling and sampling are not the 
same. Hence the need to document it.

According to my testing in Python 3.8, the version with sample is about 
10% slower than the "shuffled" helper I gave. That wouldn't be too bad 
if the operation was fast, but for a sequence of 30,000 items on my 
computer, that takes nearly half a second. So a 10% slowdown is quite 
significant.

I think I'll continue using my shuffled helper function, and while I 
personally won't re-raise this issue, I'll continue to give it my 
support next time somebody raises it.


-- 
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BDMLDARYAUQ3O3KJQ4X4JDKQWXKZREC6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to