Pregunta

I want to go through all permutations of (1,2,3,4,5) in Python and calculate something for each permutation, like

for li in PermutationLists: # PermutationLists = [[1,2,3,4,5],[1,2,3,5,4],...]
    print li[0]-li[1]+li[2]-li[3]+li[4]

What would be a convenient way to iterate through all these permutations of (1,2,3,4,5)?

¿Fue útil?

Solución

As Ashwini Chaudhary pointed out in the comments:

from itertools import permutations

permutes = permutations([1,2,3,4,5])

for li in permutes:
    # do stuff
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top