1. Finite sequence iterators

1.1 the accumulate function

Used to return the cumulative result of an operation. Similar to the reduce() function, the default operation is summation

1.1.1 Addition and Summation

from itertools import accumulate
​
print(list(accumulate(range(10))))
Copy the code

result:

[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
Copy the code

As you can see, each item that returns the result is the sum of all elements from the beginning to the current position. For example, the third element is 3=0+1+2, and the sixth element 15=0+1+2+3+4+5. So on

1.1.2 Multiplication quadrature

import operator
from itertools import accumulate
​
print(list(accumulate(range(1, 10),operator.mul)))
Copy the code

result:

[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
Copy the code

As you can see, each item that returns the result is the product of all elements from the beginning to the current position. For example, the third element is 6=1×2×3, and the sixth element 720=1×2×3×4×5×6. So on

1.2 chain function

Chain (*iterables) : iterables: iterables passed in

Concatenate multiple iterators passed into one iterator and iterate in the order passed in

from itertools import chain
​
list1 = [1, 2, 3]
str1 = 'abcd'
tuple1 = (4, 5, 6)
​
for num in chain(list1, str1, tuple1):
    print(num)
Copy the code

result:

1
2
3
a
b
c
d
4
5
6
Copy the code

1.3 chain. Form_iterable function

The usage is the same as that of chain

from itertools import chain
​
list1 = [1, 2, 3]
str1 = 'abcd'
tuple1 = (4, 5, 6)
​
for num in chain.from_iterable([list1, str1, tuple1]):
    print(num)
Copy the code

result:

1
2
3
a
b
c
d
4
5
6
Copy the code

1.4 compress function

compress(data, selector):

Data: A piece of data that can be used for iteration

Selector: a selector used to filter data

Generates a filtered iterator. The filtering rule is that when the ith value of selector is true, the ith value of data is retained. Otherwise abandon. There’s no requirement that the length of the selector be equal to the length of the data

from itertools import compress
​
list1 = [1, 2, 3, 5, 6, 7]
selector = [1, 0, 0, 2]
for num in compress(list1, selector):
    print(num)
Copy the code

result:

1, 5Copy the code

Here is an explanation of the results. We iterate over the elements in selector, get the index of that element, and then use that index to get the elements in List1.

When an element in selector is true, the element in List1 is retained. Otherwise, list1 elements are not retained. Because selector[0]=1 is true, the output remains list1[0]=1; Selector [1]=0 is false, so the output does not retain list1[1]=2. Similarly, iist1[2]=3 is not reserved, list1[4]=5 is reserved. So the final output is 1 and 5,

In addition, the length of list1 is 6, and the length of selector is 4, which means that only the first four elements of list1 are filtered. The next two elements are not filtered, so the last two elements, 6 and 7, are not filtered in the output

1.5 dropwhile function

Dropwhile (predicate, iterable) :

Predicate: A predicate function whose return value is a bool

Iterable: an iterable

Predicate is determined by substituting the elements of iterable into the predicate. If the result is True, the predicate is removed, and if it is False, the predicate is retained. Finally, a new iterator is generated

from itertools import dropwhile
​
for i in dropwhile(lambda x: x < 3, [1, 2, 3, 4, 5]):
    print(i)
Copy the code

result:

3, 4, 5Copy the code

1.6 filterfalse function

Filterfalse (predicate, iterable) :

Predicate: Predicate, a judgment function whose return value is a bool

Iterable: an iterable

Filter the elements of iterable that do not satisfy the predicate to generate a new iterator

from itertools import filterfalse
​
for i in filterfalse(lambda x: x < 3, [1, 2, 3, 4, 5]):
    print(i)
Copy the code

result:

3, 4, 5Copy the code