How to use the itertools module of Python standard library

高洛峰
Release: 2017-03-21 11:27:54
Original
1846 people have browsed it

Introduction

Official description: Functional tools for creating and using iterators. That is, functions for creating efficient iterators.

itertools.chain(*iterable)

Return multiple sequences as a single sequence.
For example:

import itertools
for each in itertools.chain('i', 'love', 'python'):
    print each
Copy after login

Output:

i
l
o
v
e
p
y
t
h
o
n
Copy after login

itertools.combinations(iterable, r)

Returns "combinations" of the specified length
For example:

import itertools
for each in itertools.combinations('abc', 2):
    print each
Copy after login

Output:

('a', 'b')
('a', 'c')
('b', 'c')
Copy after login

itertools.combinations_with_replacement(iterable, r)

Returns a "combination" of the specified length, and the elements in the combination can be repeated
For example:

import itertools
for each in itertools.combinations_with_replacement('abc', 2):
    print each
Copy after login

Output:

('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
Copy after login

itertools.product(*iterable[,repeat])

Returns all combinations of the specified length, which can be understood as the Cartesian product
For example:

import itertools
for each in itertools.product('abc', repeat=2):
    print each
Copy after login

('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
( 'c', 'c')

itertools.premutations(iteravle[,r])

Return a permutation of length r
For example:

import itertools
for value in itertools.permutations('abc', 2):
    print value
Copy after login

Output:

('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
Copy after login

itertools.compress(data,selector)

Returns the corresponding element of data whose selector is True
For example:

import itertools
for each in itertools.compress('abcd', [1, 0, 1, 0]):
    print each
Copy after login

Output:

a
c
Copy after login

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

Returns a sequence starting with start and increasing step, infinitely increasing
For example:

import itertools
for each in itertools.count(start=0, step=2):
    print each
Copy after login

Output:

1
2
3
.
.
Copy after login

itertools.cycle(iterable)

Iterate the iterator infinitely
For example:

import itertools
for each in itertools.cycle('ab'):
    print each
Copy after login

Output:

a
b
a
b
.
.
Copy after login

itertools.dropwhile(predicate, iterable)

Until predicate is true, return iterable subsequent data, otherwise drop it
For example:

import itertools
for each in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]):
    print each
Copy after login

Output:

6
8
2
1
Copy after login

itertools.groupby(iterable[,key])

Return a group (key, itera), key is the value of iterable, itera is all items equal to key
For example:

import itertools
for key, vale in itertools.groupby('aabbbc'):
    print key, list(vale)
Copy after login

Output:

a ['a', 'a']
b ['b', 'b', 'b']
c ['c']
Copy after login

itertools. ifilter(predicate, iterable)

Returns an element iterator whose predicate result is True. If predicate is None, returns all items that are True in iterable
For example:

import itertools
for value in itertools.ifilter(lambda x: x % 2, range(10)):
    print value
Copy after login

Output:

1
3
5
7
9
Copy after login

itertools.ifilterfasle(predicate,iterable)

Returns elements whose predicate is False. If predicate is None, returns all items in iterable that are False.
For example:

import itertools
for value in itertools.ifilterfalse(lambda x: x % 2, range(10)):
    print value
Copy after login

Output:

0
2
4
6
8
Copy after login

itertools.imap(function,*iterables)

Equivalent to map() in iterator mode
For example:

import itertools
for value in itertools.imap(lambda x, y: x+y, (1,2,3), (4,5,6)):
    print value
Copy after login

Output :

5
7
9
Copy after login

itertools.islice(iterable, start,stop[,step])

Equivalent to iterator mode slicing operation
For example:

import itertools
for value in itertools.islice('abcdefg', 1, 4, 2):
    print value
Copy after login

Output:

b
d
Copy after login

itertools.repeat(object,[,times])

Continuously return the object object. If times is specified, return times times
For example:

import itertools
for value in itertools.repeat('a', 2):
    print value
Copy after login

Output:

a
a
Copy after login

itertools.starmap(function,iterable)

Returns the value of function(iter), iter is the element of iterable
For example:

import itertools
for value in itertools.starmap(lambda x, y: x * y, [(1, 2), (3, 4)]):
    print value
Copy after login

Output:

2
12
Copy after login

itertools.takewhile(predicate,iterable)

If predicate is true, return the iterable element, if it is false, it will not return, break.
For example:

import itertools
for value in itertools.takewhile(lambda x: x < 5, [1, 3, 5, 6]):
    print value
Copy after login

Output:

1
3
Copy after login

The above is the detailed content of How to use the itertools module of Python standard library. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template