Combinatoric Iterators in Python

Combinatoric iterators or generators contain mainly four iterators which can be used to perform combination, permutation and products of the data. These iterators are contained in the itertools library.

Generators are also a type of iterator but unlike other iterators and functions, it cannot return values instead it can directly give result when they are called or used.

There are four combinatoric iterators in  Python that are given below:

1.Permutation(): This iterator is used for calculating all possible permutation on a given input data set. In this, each value is identified uniquely from its position.

SYNTAX:

permutations (iterable, size)

If we do not pass the value of the size or if it is Null than size becomes equal to the length of iterables.

 

2.Combination(): This iterator is used to calculate all combinations of a data set. Remember this works without replacement of any value.

SYNTAX:

combinations(iterable, size)

 

3.Combination with replacement (): This iterator is like combinations, the difference is, the result comes by repeating the values given in the data set.

SYNTAX:

combinations_with_replacement (iterable, n)

‘n’ is the size of the constructed sequence.

 

4.Product(): This iterator is used to create a Cartesian product from a given data set. In this, we tell no. Of repetition we want.

SYNTAX:

product(iterable, repetition)

 

Code(Python)

All four types of Combinatoric iterators are explained in one single program and shown how each of them actually works

 

#1 PERMUTATION
from itertools import permutations  
print ("Possible permutations are , example:") 
A=list(permutations(range(4), 2))
print(A)

#2 COMBINATION
from itertools import combinations 
print ("Possible combinations in sorted order(without replacement) are:") 
B=list(combinations(range(4),2))
print(B)

#3 COMBINATION WITH REPETITION
from itertools import combinations_with_replacement 
print ("Possible combinations in sorted order(with replacement) are:") 
C=list(combinations_with_replacement(range(4), 2))
print(C)

#4 PRODUCT
from itertools import product 
print("Possible products ,example:") 
D=list(product([3, 5], repeat=3))
print(D)

Output

Possible permutations are , example:
[(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]

Possible combinations in sorted order(without replacement) are:
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

Possible combinations in sorted order(with replacement) are:
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]

Possible products ,example:
[(3, 3, 3), (3, 3, 5), (3, 5, 3), (3, 5, 5), (5, 3, 3), (5, 3, 5), (5, 5, 3), (5, 5, 5)]
[Program finished]

 

For more information about Iterators and Generators you can visit: https://docs.python.org/3/library/itertools.html

I hope you understand what is Combinatoric iterators in Python and how to code. You can try running the code, if have any doubt, you may drop a comment.

 

Leave a Reply

Your email address will not be published. Required fields are marked *