Hướng dẫn dùng python permutations python

Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.

Show

Permutation 

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

Python3




# A Python program to print all 

# permutations using library function 

from itertools

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

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

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

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

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
5
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
6
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
7
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
8
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
2
(1, 2)
(1, 3)
(2, 3)
3

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

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

(1, 2)
(1, 3)
(2, 3)
6
(1, 2)
(1, 3)
(2, 3)
7
(1, 2)
(1, 3)
(2, 3)
8
(1, 2)
(1, 3)
(2, 3)
9
(1, 2)
(1, 3)
(2, 3)
0

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

Output: 

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

It generates n! permutations if the length of the input sequence is n. 
If want  to get permutations of length L then implement it in this way. 
 

Python3




# A Python program to print all 

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

from itertools

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

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

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

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

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
5
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
6
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
7
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
8
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
2
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
1
(1, 2)
(1, 3)
(2, 3)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
3

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

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

(1, 2)
(1, 3)
(2, 3)
6
(1, 2)
(1, 3)
(2, 3)
7
(1, 2)
(1, 3)
(2, 3)
8
(1, 2)
(1, 3)
(2, 3)
9
(1, 2)
(1, 3)
(2, 3)
0

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

Output: 

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

It 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 an input r as an input and return an object list of tuples which contain all possible combination of length r in a list form. 
 

Python3




# A Python program to print all 

# A Python program to print all 5

from itertools

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0 # A Python program to print all 9

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

# permutations using library function 1

# permutations using library function 2

# permutations using library function 3

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
6 # permutations using library function 5
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
8
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
2
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
1
(1, 2)
(1, 3)
(2, 3)
0from3

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

from5

(1, 2)
(1, 3)
(2, 3)
6
(1, 2)
(1, 3)
(2, 3)
7
(1, 2)
(1, 3)
(2, 3)
8
(1, 2)
(1, 3)
(2, 3)
9itertools0

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

Output: 

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

 

1. Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order. 
 

Python3




# A Python program to print all 

itertools5

from itertools

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0 itertools9

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

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

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

# permutations using library function 3

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
6 # permutations using library function 5
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
8
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
2
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
1
(1, 2)
(1, 3)
(2, 3)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
3

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

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

(1, 2)
(1, 3)
(2, 3)
6
(1, 2)
(1, 3)
(2, 3)
7
(1, 2)
(1, 3)
(2, 3)
8
(1, 2)
(1, 3)
(2, 3)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
20

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

Output: 

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

 

2. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination. 
 

Python3




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

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

from itertools

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0 itertools9

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

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

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

# permutations using library function 3

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
6 # permutations using library function 5
(1, 2)
(1, 3)
(2, 3)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
8
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
2
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
1
(1, 2)
(1, 3)
(2, 3)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
3

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

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

(1, 2)
(1, 3)
(2, 3)
6
(1, 2)
(1, 3)
(2, 3)
7
(1, 2)
(1, 3)
(2, 3)
8
(1, 2)
(1, 3)
(2, 3)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
20

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

Output: 

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

 

3. If we want to make a combination of the same element to the same element then we use combinations_with_replacement. 
 

Python3




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

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

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

from itertools

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
60

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

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

# permutations using library function 3

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
6
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
65
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
8
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
9
(1, 2)
(1, 3)
(2, 3)
2
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
1
(1, 2)
(1, 3)
(2, 3)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
3