MODULE PART-4

ITERTOOLS:

Combinations method in Itertools Module:

itertools.combinations will return a generator of the k-combination sequence of a list.

In other words: It will return a generator of tuples of all the possible k-wise combinations of the input list.

For Example:

If you have a list:

a = [1,2,3,4,5]

b = list(itertools.combinations(a, 2))

print b

Output:

[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]

The above output is a generator converted to a list of tuples of all the possible pair-wise combinations of the input

list a

You can also find all the 3-combinations:

a = [1,2,3,4,5]

b = list(itertools.combinations(a, 3))

print b

Output:

[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4),

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

 (2, 4, 5), (3, 4, 5)]

itertools.dropwhile

itertools.dropwhile enables you to take items from a sequence after a condition first becomes False.

def is_even(x):

    return x % 2 == 0

lst = [0, 2, 4, 12, 18, 13, 14, 22, 23, 44]

result = list(itertools.dropwhile(is_even, lst))

print(result)

 

This outputs [13, 14, 22, 23, 44].

(This example is same as the example for takewhile but using dropwhile.)

Note that, the first number that violates the predicate (i.e.: the function returning a Boolean value) is_even is, 13.

All the elements before that, are discarded.

 

itertools.takewhile

itertools.takewhile enables you to take items from a sequence until a condition first becomes False.

def is_even(x):

    return x % 2 == 0

lst = [0, 2, 4, 12, 18, 13, 14, 22, 23, 44]

result = list(itertools.takewhile(is_even, lst))

print(result)

 

This outputs [0, 2, 4, 12, 18].

Note that, the first number that violates the predicate (i.e.: the function returning a Boolean value) is_even is, 13.

Once takewhile encounters a value that produces False for the given predicate, it breaks out.

 

itertools.permutations

itertools.permutations returns a generator with successive r-length permutations of elements in the iterable.

a = [1,2,3]

list(itertools.permutations(a))

# [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

list(itertools.permutations(a, 2))

[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

GoalKicker.com Python® Notes for Professionals

287

if the list a has duplicate elements, the resulting permutations will have duplicate elements, you can use set to get

unique permutations:

a = [1,2,1]

list(itertools.permutations(a))

# [(1, 2, 1), (1, 1, 2), (2, 1, 1), (2, 1, 1), (1, 1, 2), (1, 2, 1)]

set(itertools.permutations(a))

# {(1, 1, 2), (1, 2, 1), (2, 1, 1)}

 

itertools.product

This function lets you iterate over the Cartesian product of a list of iterables.

For example,

for x, y in itertools.product(xrange(10), xrange(10)):

    print x, y

is equivalent to

 

for x in xrange(10):

    for y in xrange(10):

        print x, y

Like all python functions that accept a variable number of arguments, we can pass a list to itertools.product for

unpacking, with the * operator.

Python – Itertools.count()

Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it counts!

Syntax: itertools.count(start=0, step=1)

Parameters:
start: Start of the sequence (defaults to 0)
step: Difference between consecutive numbers (defaults to 1)

Returns: Returns a count object whose .__next__() method returns consecutive values.

Example #1: Creating evenly spaced list of numbers
itertools.count() can be used to generate infinite recursive sequences easily. Lets have a look

# Program for creating a list of

# even and odd list of integers

# using count()

  

from itertools import count

 

# creates a count iterator object

iterator =(count(start = 0, step = 2))

 

# prints a odd list of integers

print("Even list:", 

      list(next(iterator) for _ in range(5)))

  

# creates a count iterator object

iterator = (count(start = 1, step = 2))

  

# prints a odd list of integers

print("Odd list:", 

      list(next(iterator) for _ in range(5)))

Output :

Even list: [0, 2, 4, 6, 8]

Odd list: [1, 3, 5, 7, 9]

There are many functions in itertools module ,you can learn about them here:

https://medium.com/@jasonrigden/a-guide-to-python-itertools-82e5a306cdf8

 

https://docs.python.org/3/library/itertools.html

 

 

 

 

 


Comments