[Python-ideas] Shuffled

Nick Coghlan ncoghlan at gmail.com
Wed Sep 7 23:23:54 EDT 2016


On 8 September 2016 at 12:47, Danilo J. S. Bellini
<danilo.bellini at gmail.com> wrote:
> Though I agree with the argument that inexperienced developers are [usually]
> worse, that's not the case here, unless anyone here is really trying to say
> the ones arguing for or against "shuffled" are inexperienced. These ad
> hominem won't bring us anywhere.
>
> No one seem to be arguing if "shuffled" is inconsistent, but the "shuffle"
> consistency is a subject that didn't reach consensus. And the point now seem
> to be whether "shuffled" is useful or not. If I understood correctly, that
> proposal is about writing code in a more functional style (or a
> expression-oriented programming style), with a pure function for shuffling.

A pure function for shuffling already exists - random.sample()

So the question at hand boils down to whether it makes sense to provide:

    def shuffled(self, container):
        return self.sample(container, len(container))

as a method on Random objects, and equivalently as a top-level module function.

I'd personally be in favour of that as a learning bridge between
shuffling and sampling (shuffle in place -> shuffle out of place ->
sample a subset rather than shuffling out of place and slicing), but
I'm neither a professional educator nor a maintainer or heavy user of
the module in question, so my preference in the matter should be
weighted pretty low.

> I think there are other related subjects that can be said about
> sorted/shuffled. For example: why a list? Why not a generator?

This is covered in the random.sample() docs - returning a list is
useful, as you can then easily use slices and tuple unpacking to
partition the result:

    winners = random.sample(participants, 10)
    first, second, *runners_up = winners

Beyond that practical benefit, if you want
random-sampling-with-replacement, then "map(random.choice, container)"
already has you covered, while random-sampling-without-replacement
inherently needs to maintain a set of already produced values so it
can avoid repeating them, which makes the apparent memory efficiency
of using a generator instead is somewhat illusory.

> Something
> like random.choice is useful to get one single value, but if one just want
> to see a few random values from a large input without repeating the values,
> what's the best approach?

Use random.sample(), that's what it's for.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list