SoFunction
Updated on 2024-10-30

Common methods of the python iterator module itertools

preamble

itertools is built-in python is an efficient generation of a variety of iterator or class module, the return value of these functions for an iterator, often used in the for loop, of course, you can also directly use the next () method of taking the value of the itertools today to talk about the common methods.

Itertools can be categorized into three types of iterators according to their functionality.

  • Infinite Iterators.Generate an infinite sequence, such as the sequence of natural numbers 1, 2, 3, 4, ...
  • Finite Iterators.Receive one or more sequences as parameters for combining, grouping, filtering, etc;
  • Combinatorial Iterators.Arrangement and combination of sequences, finding the Cartesian product of sequences, etc.

1. Infinite Iterator

(start=0, step=1)

Create an iterator that generates consecutive integers starting from n. If n is ignored, the counting starts from 0 (note: this iterator doesn't support long integers), and if it exceeds that, the counter overflows and the counting continues from ---1.

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

Output.

1
2
3
4
5
6

b = (2,3)
for x in b:
    if x > 10:
        break
    print(x)

Output:

2
5
8

(iterable)

Creates an iterator that repeatedly performs a loop over the elements in iterable, internally generating a copy of the elements in iterable, which is used to return duplicate items in the loop

iterable: Iteratable object, can be a list, string, tuple, etc.

import itertools
a = ['a','b','c']
i = 0
for x in (a):
    i = i +1
    if i > 5:
        break
    print(i,x)

Output:

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

(object[, times])

Creates an iterator that iterates over the object, times (if supplied) specifies the repeat count, and if times is not supplied, the object is returned endlessly.

  • object: Objects that need to be duplicated, objects that are a whole
  • times: repetition
import itertools
for x in ([1,2,3],3):
    print(x)

Output:

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

2. Finite Iterators

(iterable1, iterable2, …)

By taking multiple iterators as arguments, but returning only a single iterator, it produces the contents of all argument iterators as if they were from a single sequence
Arguments are multiple iterable objects, as if chained together.

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

Output:

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

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

Output:

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

.from_iterable(iterable)

Takes an iterable object as an argument and returns 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]

(data, selectors)

# can be used to filter the data, when an element of selectors is true, the element at the corresponding position of data is kept, otherwise it is removed.
#data: data to be filtered
#selectors: when true, keep the data in the corresponding bit of data, when false or null, remove it.

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

Output:

'a'
'c'
# 2 is also true, 'd' is false if the corresponding value is null.

(predicate, iterable)

Create an iterator that discards items in iterable as long as the function predicate(item) is True. If predicate returns False, it generates items in iterable and all subsequent items, i.e., the first item that doesn't satisfy the condition and all the items that follow it return

  • predicate: function (math.)
  • iterable: iterable object (computing)
from itertools import dropwhile
list(dropwhile(lambda x: x < 5, [1, 3, 6, 2, 1]))

Output.

[6,2,1]
# Starting at 6 does not satisfy the x < 5 condition, so 6 and all items after 6 need to be returned.

(predicate, iterable)

Create an iterator, and if predicate returns False, stop iterating immediately.

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

Output:

[1,3]

(predicate, iterable)

Creates an iterator that generates only the items in iterable whose predicate(item) is True, and if predicate is None, returns all items in iterable that evaluate to True

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

Output.

[1,3,2,1]

(predicate, iterable)

Create an iterator that generates only the items in iterable whose predicate(item) is False, and if predicate is None, it will return all the items in iterable that evaluate to False, which is the opposite of ifilter.

  • predicate: function (math.)
  • iterable: iterable object (computing)
from itertools import ifilterfalse
list(ifilterfalse(lambda x: x < 5, [1, 3, 6, 2, 1]))

Output.

[6]

(iterable[, key])

Returns an iterator that produces a set of values grouped by key.

  • iterable:possibleIterative objects
  • key: A function whose return value is used as a criterion for grouping.
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']

(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, used to generate infinite sequences

(func, iter1, iter2, iter3, …)

imap(func, iter1, iter2, iter3, …)
Returns an iterator, which is a call to a function whose value is on the input iterator, returning the result. It's similar to the built-in function map(), except that it stops at the end of any input iterator (instead of inserting None to complete all inputs).
Note: This function no longer exists in the map, so you can use it directly.

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

Output:

[16, 9, 1000]
The #pow function Finds the exponent

(*iterables)

It is used to take the elements of the corresponding positions of multiple iterable objects as a tuple, 'compose' all the tuples into an iterator, and return the
Caution.This function no longer exists in zip, so you can use it directly.

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 continues until all elements of the iterable object have been iterated over.

Caution.This function no longer exists in

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. Combinatorial Iterators

*(iterables[, repeat])

Creates an iterator that generates a tuple representing the Cartesian product of the items in item1, item2, etc. repeat is a keyword argument that specifies the number of times to repeat the generated sequence.
used to generate the Cartesian product

import itertools
a = (1, 2, 3)
b = ('A', 'B', 'C')
c = (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')]

The simple use of the

from itertools import product  # Iterator

# test script
for i j in product(range(10),range(10))
    
print(i,j)
# The same is equal to two for loop nesting, only this way of writing far speed traversal will be faster, time complexity is reduced.
for x in range(10):
    for y in range(10):
    print(x,y)

(iterable[, r])

Create an iterator that returns a sequence of all items in iterable of length r. If r is omitted, then the length of the sequence is the same as the number of items in iterable: an iterator that returns a tuple of tuples of any r elements from p.

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

Output.

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

(iterable, r)

Create an iterator that returns all subsequences of length r in iterable, with the items in the returned subsequences sorted in the order in which they were entered into iterable (without duplicates).

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

Output.

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

itertools.combinations_with_replacement(iterable, r)

Create an iterator that returns all subsequences of iterable of length r. The items in the returned subsequences are sorted in the order in which they were entered into iterable (with repetition).

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')]

to this article on the python iterator module itertools commonly used methods of the article is introduced to this, more related python itertools content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!