Python standard library itertools module introduction
itertools is a built-in python module, easy to use and powerful, here to try to summarize and organize, and provide simple application examples; if you can not meet your requirements, welcome to join the supplement.
Using the Python standard library itertools is a simple one-sentence import: import itertools
chain()
As its name implies, give it a list such as lists/tuples/iterables, linked together; return the iterables object.
letters = ['a', 'b', 'c', 'd', 'e', 'f'] booleans = [1, 0, 1, 0, 0, 1] print(list((letters,booleans))) # Output: ['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1] print(tuple((letters,letters[3:]))) #output('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f') print(set((letters,letters[3:]))) # Output: {'a', 'd', 'b', 'e', 'c', 'f'} print(list((letters,letters[3:]))) # Output: ['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f'] for item in list((letters,booleans)): print(item)
count()
Generate unbounded sequence, count(start=0, step=1) , example start from 100, step=2, loop 10, print the corresponding value; must manually break, count() will keep looping.
i = 0 for item in (100,2): i += 1 if i > 10 : break print(item)
filterfalse()
Python filterfalse(contintion,data) Iteratively filters data whose condition is false. If the condition is null, return the item in data that is false;
booleans = [1, 0, 1, 0, 0, 1] numbers = [23, 20, 44, 32, 7, 12] print(list((None,booleans))) # Output: [0, 0, 0] print(list((lambda x : x < 20,numbers))) # Output: [23, 20, 44, 32]
compress()
Returns the element we need to use to return the corresponding element in the set a, based on the truth value of the element in the set b.
print(list((letters,booleans))) # ['a', 'c', 'f']
starmap()
For each item in the list, call the function function. starmap(func,list[]) ;
starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 >>> from itertools import * >>> x = starmap(max,[[5,14,5],[2,34,6],[3,5,2]]) >>> for i in x: >>> print (i) 14 34 5
repeat()
repeat(object[, times]) Repeat times times;
repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq ); when the execution of function f returns false, start iterating through the sequence
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable); returns the sequence, which is cutoff when predicate is true.
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]); returns an iterator over the elements of the sequence seq with step from start to stop
for i in islice("abcdef", 0, 4, 2):#a, c print i
product()
product(iter1,iter2, ... iterN, [repeat=1]); Creates an iterator that generates a tuple representing the Cartesian product of the items in item1, item2, etc. repeat is a keyword argument specifying the number of times to repeat the generated sequence.
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 for i in product([1, 2, 3], [4, 5], [6, 7]): print i (1, 4, 6) (1, 4, 7) (1, 5, 6) (1, 5, 7) (2, 4, 6) (2, 4, 7) (2, 5, 6) (2, 5, 7) (3, 4, 6) (3, 4, 7) (3, 5, 6) (3, 5, 7)
permutations()
permutations(p[,r]); returns an iterator over a tuple of tuples taking any r elements of p for permutations.
for i in permutations([1, 2, 3], 3): print i (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)
combinations()
combinations(iterable,r); creates 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.
note: no repetition
for i in combinations([1, 2, 3], 2): print i (1, 2) (1, 3) (2, 3)
combinations_with_replacement()
Same as above, with duplicates Example.
for i in combinations_with_replacement([1, 2, 3], 2): print i (1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)
Application Examples
Find the three numbers in the sequence of prime numbers 1,3,5,7,9,11,13,15 whose sum is 35;
def get_three_data(data_list,amount): for data in list((data_list, 3)): if sum(data) == amount: print(data) #(7, 13, 15)
For more information on how to use the python standard library, please click on the related articles below.