Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting permuations in Python

Tags:

python

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!

like image 919
Oniropolo Avatar asked May 09 '13 02:05

Oniropolo


1 Answers

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 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
like image 84
nneonneo Avatar answered Sep 29 '22 14:09

nneonneo