[Python-ideas] Extremely weird itertools.permutations

Neil Girdhar mistersheik at gmail.com
Sat Oct 12 04:37:02 CEST 2013


I think it's pretty indisputable that permutations are formally defined
this way (and I challenge you to find a source that doesn't agree with
that).  I'm sure you know that your idea of using permutations to evaluate
a multinomial distribution is not efficient.  A nicer way to evaluate
probabilities is to pass your set through a collections.Counter, and then
use the resulting dictionary with scipy.stats.multinomial (if it exists
yet).

I believe most people will be surprised that len(permutations(iterable))
does count unique permutations.

Best,

Neil


On Fri, Oct 11, 2013 at 10:06 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> On Fri, Oct 11, 2013 at 11:38:33AM -0700, Neil Girdhar wrote:
> > "It is universally agreed that a list of n distinct symbols has n!
> > permutations. However, when the symbols are not distinct, the most common
> > convention, in mathematics and elsewhere, seems to be to count only
> > distinct permutations." —
>
> I dispute this entire premise. Take a simple (and stereotypical)
> example, picking balls from an urn.
>
> Say that you have three Red and Two black balls, and randomly select
> without replacement. If you count only unique permutations, you get only
> four possibilities:
>
> py> set(''.join(t) for t in itertools.permutations('RRRBB', 2))
> {'BR', 'RB', 'RR', 'BB'}
>
> which implies that drawing RR is no more likely than drawing BB, which
> is incorrect. The right way to model this experiment is not to count
> distinct permutations, but actual permutations:
>
> py> list(''.join(t) for t in itertools.permutations('RRRBB', 2))
> ['RR', 'RR', 'RB', 'RB', 'RR', 'RR', 'RB', 'RB', 'RR', 'RR', 'RB', 'RB',
> 'BR', 'BR', 'BR', 'BB', 'BR', 'BR', 'BR', 'BB']
>
> which makes it clear that there are two ways of drawing BB compared to
> six ways of drawing RR. If that's not obvious enough, consider the case
> where you have two thousand red balls and two black balls -- do you
> really conclude that there are the same number of ways to pick RR as BB?
>
> So I disagree that counting only distinct permutations is the most
> useful or common convention. If you're permuting a collection of
> non-distinct values, you should expect non-distinct permutations.
>
> I'm trying to think of a realistic, physical situation where you would
> only want distinct permutations, and I can't.
>
>
> > Should we consider fixing itertools.permutations and to output only
> unique
> > permutations (if possible, although I realize that would break code).
>
> Absolutely not. Even if you were right that it should return unique
> permutations, and I strongly disagree that you were, the fact that it
> would break code is a deal-breaker.
>
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/dDttJfkyu2k/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131011/e0b1a2b1/attachment-0001.html>


More information about the Python-ideas mailing list