[Python] Counting permuations in Python

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    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 =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!

    Continue reading...

Compartilhe esta Página