A few weeks back, I was solving a question and all solutions I came up with proved abortive. I approached a friend on how to solve it and he came up with a very apt, accurate and fast solution, which he had to import a module called ‘ITERTOOLS’ for. Prior to this time, I never knew this module existed as I’m still learning (we all are actually). At that point I was just concerned about solving the problem, but afterwards I went to look up on google to know more on itertool, which brings me to writing on it.
What is Itertool ?
Itertool is a module that provides various functions that works on iterators to produce complex iterators. This module works as a fast, memory- efficient tool, that is used either by themselves or in combination to form iterator algebra.
What is a Module ?
Modules are files which have a set of functions, classes or variables you want to include in your application. We can also say modules provide us with the flexibility to organize the code in a logical way.
What are Iterators ?
Iterators are objects used to loop or traverse through iterable objects like lists, tuples, strings, sets and dictionaries.
Now with module and iterators defined, hope we understand what an itertool does. Itertool helps us to solve complex problems easily with the help of different sub-functions.
The sub-functions are divided into three (3), which are :
Infinite Iterators
Terminating Iterators
Combinatorics Generator
For today, we will be looking at the particular itertool that I used in solving my problem, which is Itertools.combinations() Itertools.combination() falls under the third subcategory called “Combiantoric Generators”. Combinatoric Generators are those iterators that are used to simplify combinatorial constructs such as permutations, combiantions, Cartesian products. Itertools.combinations provides us with all the possible tuples, a sequence or set of numbers or letters used in the iterator and the elements are assumed to be unique on the basis of their positions which are distinct for all elements.
All these combinations are emitted in lexicographical order. This function takes ‘r’ as input, ‘r’ here represents the size of different combinations that are possible. All the combinations emitted are of length ‘r’ and ‘r’ is a necessary argument here.
Syntax : Before you can use the combinations function, you have to import it from the Itertool module as seen in the example below. combinations(iterator, r)
We will be looking at two examples to help understand what Itertools actually do, one will be simple, while the other one will be my aforementioned problem I was struggling with…………let’s gooooo Examples:
Combinations Of string "GeEKS" OF SIZE 3.
from itertools import combinations
letters ="GeEKS"
# size of combination is set to 3
a = combinations(letters, 3)
y = [' '.join(i) for i in a]
print(y)
Output:- ['G e E', 'G e K', 'G e S', 'G E K', 'G E S', 'G K S', 'e E K', 'e E S', 'e K S', E K S’]
This example gives us the possible number of combinations of strings we can get from the letters.
- Problem – complete the function that finds the maximum substring that can be gotten in alphabetical order from a given string Where s = ‘baca’
The substrings are : [ ‘b’, ‘ba’, ‘bac’, ‘baca’, ‘a’, ‘ac’, ‘aca’, 'c', ‘ca’] Solution:
from itertools import combinations
def maxSubstring(s):
substrings = [s[x:y] for y in combinations(range(len(s) + 1, r =2)]
return sorted(substrings)[-1]
The above function returns the maximum substring that can be gotten from the string.
I have found itertool very useful, and suggested it to people who found it useful.
Reference - %[geeksforgeeks.org/python-itertools-combinat..