Common methods of python iterator module itertools

Itertools is a built-in module in python that generates various iterators or classes efficiently. The return value of these functions is an iterator, which is often used in the for loop. Of course, you can also directly use the next() method to get values. Today, let's talk about the common methods in itertools

itertools can be divided into three categories according to the functions of iterators:

  • Infinite iterator: Generate an infinite sequence, such as natural number sequence 1, 2, 3, 4
  • Finite iterator: receives one or more sequence s as parameters for combination, grouping and filtering;
  • Combination iterator: arrangement and combination of sequences, Cartesian product of sequences, etc

1. Infinite iterator

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

Create an iterator to generate consecutive integers starting from N. if n is ignored, the calculation starts from 0 (Note: This iterator does not support long integers). If it exceeds sys.maxint, the counter will overflow and continue to calculate from - sys.maxint-1

  • Start: start value, default is 0
  • Step: step size, 1 by default
import itertools
a = itertools.count()
for x in a:
    if x > 5:
        break
    print(x)

Output:

1
2
3
4
5
6
b = itertools.count(2,3)
for x in b:
    if x > 10:
        break
    print(x)

output

2
5
8

itertools.cycle(iterable)

Create an iterator to repeat the loop operation on the elements in iterable. A copy of the elements in iterable will be generated internally. This copy is used to return the duplicates in the loop

  • iterable: an iterative object, which can be a list, string, tuple, etc
import itertools
a = ['a','b','c']
i = 0
for x in itertools.cycle(a):
    i = i +1
    if i > 5:
        break
    print(i,x)

output

1,'a'
2,'b'
3,'c'
4,'a'
5,'b'

itertools.repeat(object[, times])

Create an iterator and repeatedly generate the object. Times (if provided) specifies the repetition count. If times is not provided, the object will be returned endlessly

  • Object: a duplicate object is required. The object is a whole
  • Times: repetition times
import itertools
for x in itertools.repeat([1,2,3],3):
    print(x)

output

[1,2,3]
[1,2,3]
[1,2,3]

2. Finite iterator

itertools.chain(iterable1, iterable2, ...)

Take multiple iterators as parameters, but only return a single iterator, which generates the contents of all parameter iterators as if they were from a single sequence
Parameters are multiple iterative objects, as if they are connected by chains

import itertools
for x in itertools.chain([1,2,3],'abc'):
    print(x)

output

1
2
3
'a'
'b'
'c'
for x in itertools.chain([1,2,3],['a','b','c']):
    print(x)

output

1
2
3
'a'
'b'
'c'

itertools.chain.from_iterable(iterable)

Receive an iterable object as a parameter and return an iterator

from itertools import chain
a = [['first','second','thrid'],['a','b','c']]
b = [[1,2,3],[4,5,6]]
for x in range(len(a)):
    list(chain.from_iterable(zip(a[x],b[x])))

output

['first', 1, 'second', 2, 'thrid', 3]
['a', 4, 'b', 5, 'c', 6]

itertools.compress(data, selectors)

#It can be used to filter data. When an element of selectors is true, the element at the corresponding position of data will be retained, otherwise, it will be removed
#Data: data to be filtered
#selectors: When it is true, the data corresponding to the data bit is reserved; when it is false or empty, it is removed

from itertools import compress
for x in compress(['a','b','c','d'],[1,0,2]):
    print(x)

output

'a'
'c'
# 2 is also true, and the corresponding value of'd 'is null and false

itertools.dropwhile(predicate, iterable)
Create an iterator. As long as the function predicate(item) is True, discard the items in the iterable. If predicate returns False, the items in the iterable and all subsequent items will be generated, that is, the first item that does not meet the condition and all subsequent items will be returned

  • Predict: function
  • Iterable: iterable object
from itertools import dropwhile
list(dropwhile(lambda x: x < 5, [1, 3, 6, 2, 1]))

Output:

[6,2,1]
#Starting from 6, the condition of x < 5 is not met, so all items after 6 and 6 need to be returned

itertools.takewhile(predicate, iterable)

Create an iterator. If predict returns False, stop the iteration immediately

from itertools import takewhile
list(takewhile(lambda x: x < 5, [1, 3, 6, 2, 1]))

output

[1,3]

itertools.ifilter(predicate, iterable)
Create an iterator to generate only the items whose predicate(item) is True in iterable. If predicate is None, all the items in iterable calculated to be True will be returned

  • predicate: function
  • Iterable: iterable object
from itertools import ifilter
list(ifilter(lambda x: x < 5, [1, 3, 6, 2, 1]))

Output:

[1,3,2,1]

itertools.ifilterfalse(predicate, iterable)

Create an iterator to generate only items in iterable whose predicate(item) is False. If predicate is None, all items in iterable whose False is calculated will be returned. This function is just opposite to ifilter

  • Predict: function
  • Iterable: iterable object
from itertools import ifilterfalse
list(ifilterfalse(lambda x: x < 5, [1, 3, 6, 2, 1]))

Output:

[6]

itertools.groupby(iterable[, key])

Returns an iterator that generates a set of values grouped by key

  • iterable: iteratable object
  • key: a function whose return value is used as the grouping standard
'''
No one answers any questions in your study? Xiaobian created a Python Learning exchange group: 711312441
 Find small partners with similar interests and help each other,There are also good video learning tutorials and PDF e-book!
'''
from itertools import groupby
a = ['aa', 'ab', 'abc', 'bcd', 'abcde']
for i, k in groupby(a, len):
     print (i, list(k))

output

2,['aa', 'ab']
3,['abc', 'bcd']
5,['abcde']

itertools.islice(iterable, stop)
Iterable is an iterable object, start is the start index, default is 0, stop is the end index, step is the step size, default is 1, start and step are optional

from itertools import islice,count
list(islice([10, 6, 2, 8, 1, 3, 9], 5))

output

[[10, 6, 2, 8, 1]
list(islice(count(), 3, 10 ,2))

output

 [3,5,7,9]
 #Here, count() is the first function of the article, which is used to generate an infinite sequence

itertools.imap(func, iter1, iter2, iter3, ...)

imap(func, iter1, iter2, iter3, ...)
Returns an iterator. It calls a function whose value is on the input iterator and returns the result It is similar to the built-in function map(), except that the former stops after any input iterator ends (instead of inserting a None value to complete all inputs)
Note: this function no longer exists in python3.x. you can use map directly

from itertools import imap
list(imap(pow, [2, 3, 10], [4, 2, 3]))

output

[16, 9, 1000]
#Finding exponent with pow function

itertools.izip(*iterables)

It is used to take the element at the corresponding position of multiple iteratible objects as a tuple, and "compose" all tuples into an iterator, and return
Note: This function no longer exists in python 3. x, you can directly use zip

from itertools import izip
for item in izip([1, 2, 3], ['a', 'b', 'c', 'd', 'e']):
    print(item)

output

(1, 'a')
(2, 'b')
(3, 'c')

*itertools.izip_longest(iterables, [fillvalue=None])

izip_longest is similar to izip, but the iteration process will continue until all the elements of the iterable objects are iterated

Note: this function no longer exists in python3.x

from itertools import izip_longest
for item in izip_longest([1, 2, 3], ['a', 'b', 'c', 'd', 'e'],fillvalue='-'):
    print(item)

output

(1, 'a')
(2, 'b')
(3, 'c')
('-','d')
('-','e')

3. Combined iterator

*itertools.product(iterables[, repeat])

Create an iterator to generate a tuple representing the Cartesian product of items in item1, item2, etc. repeat is a keyword parameter that specifies the number of times the generation sequence is repeated.
Used to generate Cartesian product

'''
No one answers the questions in your study? Xiao Bian created a Python Learning exchange group: 711312441
 Find small partners with similar interests and help each other,There are also good video learning tutorials and PDF e-book!
'''
import itertools
a = (1, 2, 3)
b = ('A', 'B', 'C')
c = itertools.product(a,b)
for elem in c:
    print(elem)

output

(1, 'A')
(1, 'B')
(1, 'C')
(2, 'A')
(2, 'B')
(2, 'C')
(3, 'A')
(3, 'B')
(3, 'C')
list(product((0,1), (0,1), (0,1)))

output

[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
list(product('ABC', repeat=2))

output

[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]

Simple use of itertools.product:

from itertools import product  # iterator 

# test script
for i j in product(range(10),range(10))
    
print(i,j)

# Similarly, two for loops are nested. However, this writing method will speed up the traversal and reduce the time complexity.

for x in range(10):
    for y in range(10):
    print(x,y)

itertools.permutations(iterable[, r])
Create an iterator and return the sequence of all items with the length of r in iterable. If r is omitted, the length of the sequence is the same as the number of items in iterable: return the iterator of tuples arranged by any r elements in p

from itertools import permutations
list(permutations('ABC', 2))

output

[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]

itertools.combinations(iterable, r)

Create an iterator to return all subsequences with the length of r in iterable, and the items in the returned subsequences are sorted according to the order entered in iterable (without repetition)

from itertools import combinations
list(combinations('ABC', 2))

output

[('A', 'B'), ('A', 'C'), ('B', 'C')]

itertools.combinations_with_replacement(iterable, r)

Create an iterator to return all subsequences of length r in the iterable. The items in the returned subsequence are sorted in the order of the input iterable (with duplicates)

from itertools import combinations_with_replacement
list(combinations_with_replacement('ABC', 2))

output

[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

Tags: Python programming language

Posted by desithugg on Tue, 13 Sep 2022 22:00:57 +0530