preamble
Iterable objects are like water in a closed container, you can't pour out what you have.
itertools is a built-in standard module for python that provides a lot of clean and efficient specialized functionality that, when used properly, can greatly simplify the number of lines of code, and all methods implement generator functions, which means great memory savings.
The functionality provided by itertools is divided into three main blocks, taking the latest version 3.10 as an example:
- Infinite iteration on an iterable object, infinite output
- Finite Iteration on Iterable Objects
- Arranging Combinations of Iterable Objects
The methodology is as follows:
import package
>>> from iteratortools import *
Infinite Iteration
(start=0, step=1)
Numeric generator, you can specify the starting position and step size, and the step size can be a floating point number. Infinite output, always accumulate, in the example need to sleep 1s while output.
>>> import time >>> iterator = count(4, 0.5) >>> for i in iterator: ... print(i) ... (1) ... 4 4.5 5.0 5.5 6.0 6.5 7.0 7.5
(iteratorable)
Infinite loop to retrieve the elements of an iterable object.
>>> a = cycle("ABCD") >>> import time >>> for i in a: ... print(i) ... (1) ... A B C D A B C D
(object[, times])
Outputs the entire object over and over again, if the number of repetitions is specified, then the specified number of repetitions will be output, otherwise it will be repeated indefinitely.
>>> iterator = repeat('hello world', 10) >>> >>> for i in iterator: ... print(i) ... hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world
With this artifact, there is a new solution to the problem of outputting hello world 10 times.
finite iteration
(iteratorable[, func, *, initial=None])
Returns an item-by-item operation on the elements of the list, the operation has:
- Accumulate, returns a list of items to be accumulated to each item
- Multiply, returns a list of items to multiply to each item
- Minimum value, return to the minimum value of the current item
- Maximum value, return to the maximum value of the current item
>>> [2, 4, 8, 1, 3, 5] [2, 4, 8, 1, 3, 5] >>> arr = [2, 4, 8, 1, 3, 5] >>> >>> add = accumulate(arr) >>> >>> list(add) [2, 6, 14, 15, 18, 23] >>> >>> max = accumulate(arr, max) >>> list(max) [2, 4, 8, 8, 8, 8] >>> >>> import operator >>> mul = accumulate(arr, ) >>> list(mul) [2, 8, 64, 64, 192, 960] >>> >>> min = accumulate(arr, min) >>> list(min) [2, 2, 2, 1, 1, 1]
(*iteratorables)
Constructs multiple iterable objects into a new iterable object that is returned uniformly. Similar to chaining multiple objects into a single string
>>> iterator = chain([1,2,3],['a','b','c'],(5,6,7)) >>> list(iterator) [1, 2, 3, 'a', 'b', 'c', 5, 6, 7]
Advantage: you can integrate multiple iterable objects into one, avoiding the need to take values one by one.
chain.from_iteratorable(iteratorable)
Returns all the elements of an iterated object in a uniform way, similar to chain.
>>> chain.from_iteratorable(['abc','def']) < object at 0x1083ae460> >>> iterator = chain.from_iteratorable(['abc','def']) >>> list(iterator) ['a', 'b', 'c', 'd', 'e', 'f']
(data, selectors)
Filter elements by truth table
>>> arr = [1,2,3,4] >>> selectors = [1,0,1,0] >>> >>> iterator = compress(arr, selectors) >>> >>> list(iterator) [1, 3]
(predicate, iteratorable)
Filter by condition, discarding all elements before the first time the condition is not met
>>> arr = [1,2,3,2,1,2,1] >>> iterator = dropwhile(lambda x: x<3, arr) >>> list(iterator) [3, 2, 1, 2, 1]
(predicate, iteratorable)
Filter the elements in the iterable object according to the predicate condition, return as long as the element is true, and exit the first time it encounters a condition that is not met.
Filters by condition, discarding elements after the first encounter that don't meet the condition. The behavior is similar to the previous dropwhile, but the difference is in the choice of what to drop.
(predicate, iteratorable)
Keep the elements that don't match the condition and return the iterator
>>> arr = [1,2,3,4,5] >>> iterator = filterfalse(lambda x:x<3, arr) >>> list(iterator) [3, 4, 5]
(iteratorable, key=None)
Categorize the elements according to the specified conditions. Output conditions and elements that match them
>>> iterator = groupby(arr, lambda x: x>3) >>> for condition ,numbers in iterator: ... print(condition, list(numbers)) ... False [1, 2, 3] True [4, 5]
(iteratorable, start, stop[, step])
Slicing the iterator, you can't specify start and stop and step size in the old version, but you can in the new version.
>>> iterator = count() >>> slice_iterator = islice(iterator, 10, 20, 2) >>> list(slice_iterator) [10, 12, 14, 16, 18]
(function, iteratorable)
Apply function to an iterable object, similar to the map function.
(iteratorable, n=2)
Return n independent iterators from an iterable object
>>> iterator = tee(arr) >>> for i in iterator: ... print(type(i), list(i)) ... <class 'iteratortools._tee'> [1, 2, 3, 4, 5] <class 'iteratortools._tee'> [1, 2, 3, 4, 5]
iteratortools.zip_longest(*iteratorables, fillvalue=None)
Creates an iterator that collects elements from each iterable object. If the length of an iterable object is not aligned, the missing values will be filled according to fillvalue.
Iteration continues until the longest iterable object is exhausted. Roughly equivalent:
>>> iterator = zip_longest("ABCD", "xy", fillvalue="-") >>> list(iterator) [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]
permutation (math.)
(*iteratorables, repeat=1)
Generating the Cartesian product of multiple iterable objects
This is roughly equivalent to a nested loop in a generator expression. For example, product(A, B) and ((x,y) for x in A for y in B) return the same result.
>>> iterator = product("123", "abc") >>> list(iterator) [('1', 'a'), ('1', 'b'), ('1', 'c'), ('2', 'a'), ('2', 'b'), ('2', 'c'), ('3', 'a'), ('3', 'b'), ('3', 'c')]
Set the optional parameter repeat to the number of times to repeat. For example, product(A, repeat=4) is the same as product(A, A, A, A, A)
(iteratorable, r=None)
Generate an arrangement of length r from iteratorable elements. The arrangement of elements is analogous to giving a [1,2,3] and picking two of the elements, how many ways are there to combine them? The position of the elements after the permutation is not required.
>>> iter = permutations([1,2,3], r=3) >>> list(iterator) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
This method is perfect for solving the full alignment problem in algorithms and is simply tailor-made. If I had known that it was so simple, I wouldn't have taken the algorithm test that year. I'm not sure if you're a good person or a good person.
See leetcode question 46:buckle
(iteratorable, r)
Returns a subsequence of length r consisting of the elements of the input iteratorable. The elements are not reusable. Subsequent sequences require that the relative positions of the elements remain unchanged after and before they are aligned; if 3 comes after 1 in 1, 2, 3, then 3 must also come after 1 in the subsequence.
>>> iterator = combinations([1,2,3,4], r = 3) >>> list(iterator) [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] >>> iterator = combinations([1], r = 3) >>> list(iterator) []
This method curves the total number of combinations
buckle
iteratortools.combinations_with_replacement(iteratorable, r)
Returns a subsequence of length r consisting of the elements of the input iteratorable, allowing each element to be repeated.
>>> iter = combinations_with_replacement([1,2,3,4], r=2) >>> list(iter) [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)] >>> iterator = combinations_with_replacement([1], r=3) >>> list(iterator) [(1, 1, 1)]
to this article on the analysis of Python comes with a strong performance of the standard library itertools article is introduced to this, more related to Python standard library 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!