[Python-ideas] Shuffled
Sven R. Kunze
srkunze at mail.de
Wed Sep 7 18:10:43 EDT 2016
On 07.09.2016 02:49, Chris Kaynor wrote:
> I'll weigh in and say that I've had a few cases where I've wanted a
> shuffled function, but not many. The vast majority are interactive
> uses, where I want to get a sampling of data, and in those cases I'm
> normally just printing the output (often, by letting the REPL handle it).
>
> I'm fairly sure I've never wanted a shuffled in the actual code, and
> shuffle is almost always what I want (or just pulling items at random).
>
> Probably the most common case is to produce a list of random numbers
> in a (small) range. The current code looks roughly like:
>
> import random
> items = list(range(10))
> random.shuffle(items)
> items # this is interactive, so this prints it for me
>
> As this does not come up often, I almost invariably write the
> following first:
> import random
> random.shuffle(range(10))
>
> Then get no output and write the first form. It is not a major
> difference, and only comes up maybe a few times a year at most for me.
>
That sounds extremely familiar. I would say the interactive session is
the clear use-case here. But don't ask me why I need a shuffled list of
somethings (probably for some highly vicious and immoral reasons ;-) ).
@David
Your idea of a PyPI package could almost work. However, in an
interactive python console, I expect as much batteries included as
possible to make it as quick as possible. And considering how simple
such wrapper would be, it almost does not warrant the download of a
third-party package.
@Tim
Of course it's easy to write a wrapper function. But from what I gather
here, this is not the point. In interactive sessions, I find it highly
annoying when I need to define my own functions. Especially because I
need to do it again in a new python session.
Somebody provided a one-line hack using "sorted" to emulate "shuffled".
The statement basically is: shuffling is a special kind of sorting. So,
I would expect the interface to work the same. That at least suffices
for me to understand Arek's point of view.
I would even go so far as to say:
shuffled(my_list) # returns a new shuffled list
my_list.shuffle() # shuffles in-place
Allowing to plug-in the RNG, when needed.
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160908/30174b70/attachment-0001.html>
More information about the Python-ideas
mailing list