Cara menggunakan permutations with replacement python

In this tutorial, we will learn how to get the permutation and combination of a given data using Python. We will use Python inbuilt package to find the permutation and combination of a given number.

Permutation and combination are an essential part in mathematics. Python provides the itertools library that has the in-built functions to calculate permutation and combination.

Importing the Required library

To calculate the permutation and combination, we need to import the itertools library. We can import it using the below command.

The above statement will import the itertools library and forms a pathway to its function.

Now, we need to create the list of a sequence as an input. This list of input will return the tuple which consists of permutation and combination. We can also set the length of the permutation and combination.

A permutation is an arrangement of a set where order does matter. Python itertools module provide inbuilt permutation() method to find the permutation. Let's understand the following example.

Example -

Output:

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

In the above code, we have imported the itertools module. We called the permutation() method which takes string as an argument and provides an itertools object. It is necessary to use for loop to get the each permutation.

Let's take two sets of permutation.

Example - 2

Output:

('A', 'B')
('A', 'C')
('B', 'C')

Example - 3

Output:

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

In the above code, we have got the combination of the multiple integer number.

Permutation of the fixed length

We can calculate the permutation of the fixed length set where we only take a specified number of each element permutation. Let's understand the following example.

Example -

Output:

('H', 'e')
('H', 'l')
('H', 'l')
('H', 'o')
('e', 'H')
('e', 'l')
('e', 'l')
('e', 'o')
('l', 'H')
('l', 'e')
('l', 'l')
('l', 'o')
('l', 'H')
('l', 'e')
('l', 'l')
('l', 'o')
('o', 'H')
('o', 'e')
('o', 'l')
('o', 'l')

In the above code, we have calculated the fixed permutation by passing length as two.

Combination of String

Combination is a collection of the element where the order doesn't matter. Python itertools module provides the combination() method to calculate the combination of given data. We can calculate the combination of a string. Let's understand the following example.

Example -

Output:

('A', 'B')
('A', 'C')
('B', 'C')

Combination with Replacement

The itertools module consists of another method called combination_with_replacement() which takes under consideration the combination of a number itself as well. Let's understand its example.

Combination of Numeric Set

Output:

('J', 'J')
('J', 'a')
('J', 'v')
('J', 'a')
('J', 't')
('J', 'p')
 ('J', 'o')
('J', 'i')
('J', 'n')
('J', 't')
('a', 'a')
('a', 'v')
('a', 'a')
('a', 't')
('a', 'p')
('a', 'o')
('a', 'i')
('a', 'n')
('a', 't')
('v', 'v')
('v', 'a')
('v', 't')
('v', 'p')
('v', 'o')
('v', 'i')
('v', 'n')
('v', 't')
('a', 'a')
('a', 't')
('a', 'p')
('a', 'o')
('a', 'i')
('a', 'n')
('a', 't')
('t', 't')
('t', 'p')
('t', 'o')
('t', 'i')
('t', 'n')
('t', 't')
('p', 'p')
('p', 'o')
('p', 'i')
('p', 'n')
('p', 't')
('o', 'o')
('o', 'i')
('o', 'n')
('o', 't')
('i', 'i')
('i', 'n')
('i', 't')
('n', 'n')
('n', 't')
('t', 't')

Combination of Numeric Set

If the given input is in the sorted order, the combination tuples will be returned in sorted order. Let's understand the following example.

Example -

Output:

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

In this tutorial, we have discussed the itertools module to find the permutation and combination of the given data using the Python script.

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

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