Counting permuations in Python

Question:

What is the fastest way of counting the number of permutations? I have the following problem:

First I have this:

ncombos = itertools.combinations_with_replacement(['a1', 'a2', 'a3'], years*n)
('a1', 'a1', 'a1')
('a1', 'a1', 'a2')
('a1', 'a1', 'a3')
('a1', 'a2', 'a2')
.... etc.....    
('a3', 'a3', 'a3')

The aim is to go through each one and calculate the number of permutations that each one has and construct an array with these values. I implemented this using:

nodes = np.ones(len(leafs)); i=0  #This will store the number of permutations

for j in ncombos:
    nodes[i] =len(list(set(itertools.permutations(np.asanyarray(j), n))))
    i = i+1

np.asanyarray(j) converts the (‘a1′,’a1′,’a1’) into formal [‘a1′,’a1’, ‘a1’] which is need for permutations() to work. set erases the permutations which are identical. list makes a list of this. len calculates how many permutations can i make with a1, a1, a1.

So basically all I want is to count the number of permutations… However my code is extremely!!! slow ! Thank you!

Asked By: Oniropolo

||

Answers:

If you want permutations with replacement, this exists and is called the cartesian product. Itertools has a function for this, product():

>>> for i in itertools.product('ABC', repeat=3):
...     print i
...
('A', 'A', 'A')
('A', 'A', 'B')
('A', 'A', 'C')
('A', 'B', 'A')
('A', 'B', 'B')
('A', 'B', 'C')
('A', 'C', 'A')
('A', 'C', 'B')
('A', 'C', 'C')
('B', 'A', 'A')
('B', 'A', 'B')
('B', 'A', 'C')
('B', 'B', 'A')
('B', 'B', 'B')
('B', 'B', 'C')
('B', 'C', 'A')
('B', 'C', 'B')
('B', 'C', 'C')
('C', 'A', 'A')
('C', 'A', 'B')
('C', 'A', 'C')
('C', 'B', 'A')
('C', 'B', 'B')
('C', 'B', 'C')
('C', 'C', 'A')
('C', 'C', 'B')
('C', 'C', 'C')
Answered By: Patashu

Use math. The number of permutations of a list is the factorial of the length of the list, divided by the product of the factorials of the multiplicity of each element (since sets of repeated elements are permuted with no effect).

import operator
from collections import Counter
from functools import reduce
from math import factorial
def npermutations(l):
    num = factorial(len(l))
    mults = Counter(l).values()
    den = reduce(operator.mul, (factorial(v) for v in mults), 1)
    return num / den

Examples:

>>> npermutations([1,1,1])
1
>>> npermutations([1,2,3])
6
>>> npermutations([1,3,1,2,1,3,1,2])
420
Answered By: nneonneo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.