  Permutation and combination in Python

Python Methods and Functions

permutation

First import of the itertools package to implement permutation method in python. This method takes a list as input and returns a list of tuple objects that contain all permutations in the form of a list.

 # Python program to print everyone # permutations using a library function from itertools import permutations    # Get all permutations from [1, 2, 3] perm = permutations ([ 1 , 2 , 3 ])    # Print received permutations for i in list (perm): print i

Exit

(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)

This generates! permutations if the length of the input sequence is n.

If you want to get permutations of length L, then implement it as follows.

 # Program Python to print all # permutations of a given length from itertools import permutations   # Get all permutations of length 2 # and length 2 perm = permutations ([ 1 , 2 , 3 ], 2 )   # Print the resulting permutations for i in list (perm): print i

Exit

(1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)

This generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.

combination

This method takes a list and input r as input and returns a list of tuple objects that contain all possible combinations of length r in the form of a list.

 # Python program to print everyone # combinations of specified length from itertools import combinations   # Get all combinations [1, 2, 3] # and length 2 comb = combinations ([ 1 , 2 , 3 ], 2 )   # Print the resulting combinations for i in list (comb): print i

Exit

(1, 2) (1, 3) (2, 3)
1. The combinations are displayed in the lexicographic sort order of the input. Thus, if the input list is sorted, the combined tuples will be created in sorted order.

 # Python program to print all combinations # of a given length with unsorted input. from itertools import combinations   # Get all combinations [2, 1, 3] # and length 2 comb = combinations ([ 2 , 1 , 3 ], 2 )   # Print received combinations for i in list (comb): print i

Exit

(2, 1) (2, 3) (1, 3)
2. Items are treated as unique depending on on their position, not on their value. Therefore, if the input elements are unique, there will be no duplicate values ​​in each combination.

 # Python program to print all combinations # of a given length with duplicate inputs from itertools import combinations   # Get all combinations [1, 1, 3] # and length 2 comb = combinations ([ 1 , 1 , 3 ], 2 )    # Print the resulting combinations for i in list (comb): print i

Exit

(1, 1) (1, 3) (1, 3)
3. If we want to create a combination of the same element with the same element, we use a combination of combinations of combinations of elements.

 # Python program to print all combinations # with a combination of elements to yourself # also included from itertools import combinations_with_replacement   # Get all combinations of [1, 2, 3] and length 2 comb = combinations_with_replacement ([ 1 , 2 , 3 ], 2 )   # Print the resulting combinations for i in list (comb): print i

Exit

(1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)