Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate all permutations of a list?

How do you generate all the permutations of a list in Python, independently of the type of elements in that list?

For example:

permutations([]) []  permutations([1]) [1]  permutations([1, 2]) [1, 2] [2, 1]  permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] 
like image 744
Ricardo Reyes Avatar asked Sep 19 '08 18:09

Ricardo Reyes


People also ask

How do you find all permutations in a list?

The below example takes an empty list to store all permutations of all possible length of a given list. extend() function is used to add items to the empty list one after the other. Iterating over the elements of the list using for loop, the itertools. permutations() function finds all the possible lengths.

How do you generate all permutations of an array?

You take first element of an array (k=0) and exchange it with any element (i) of the array. Then you recursively apply permutation on array starting with second element. This way you get all permutations starting with i-th element.

How do you make a list permutation in Python?

The number of permutations on a set of n elements is given by n!. For example, there are 2! = 2*1 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 3*2*1 = 6 permutations of {1, 2, 3}, namely {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}.


1 Answers

There's a function in the standard-library for this: itertools.permutations.

import itertools list(itertools.permutations([1, 2, 3])) 

If for some reason you want to implement it yourself or are just curious to know how it works, here's one nice approach, taken from http://code.activestate.com/recipes/252178/:

def all_perms(elements):     if len(elements) <=1:         yield elements     else:         for perm in all_perms(elements[1:]):             for i in range(len(elements)):                 # nb elements[0:1] works in both string and list contexts                 yield perm[:i] + elements[0:1] + perm[i:] 

A couple of alternative approaches are listed in the documentation of itertools.permutations. Here's one:

def permutations(iterable, r=None):     # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC     # permutations(range(3)) --> 012 021 102 120 201 210     pool = tuple(iterable)     n = len(pool)     r = n if r is None else r     if r > n:         return     indices = range(n)     cycles = range(n, n-r, -1)     yield tuple(pool[i] for i in indices[:r])     while n:         for i in reversed(range(r)):             cycles[i] -= 1             if cycles[i] == 0:                 indices[i:] = indices[i+1:] + indices[i:i+1]                 cycles[i] = n - i             else:                 j = cycles[i]                 indices[i], indices[-j] = indices[-j], indices[i]                 yield tuple(pool[i] for i in indices[:r])                 break         else:             return 

And another, based on itertools.product:

def permutations(iterable, r=None):     pool = tuple(iterable)     n = len(pool)     r = n if r is None else r     for indices in product(range(n), repeat=r):         if len(set(indices)) == r:             yield tuple(pool[i] for i in indices) 
like image 195
Eli Bendersky Avatar answered Sep 23 '22 13:09

Eli Bendersky