Home  >  Article  >  Backend Development  >  How to use the itertools module of Python standard library

How to use the itertools module of Python standard library

高洛峰
高洛峰Original
2017-03-21 11:27:541725browse

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

Output:

i
l
o
v
e
p
y
t
h
o
n

itertools.combinations(iterable, r)

Returns "combinations" of the specified length
For example:

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

Output:

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

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

Output:

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

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

('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

Output:

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

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

Output:

a
c

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

Output:

1
2
3
.
.

itertools.cycle(iterable)

Iterate the iterator infinitely
For example:

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

Output:

a
b
a
b
.
.

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

Output:

6
8
2
1

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)

Output:

a ['a', 'a']
b ['b', 'b', 'b']
c ['c']

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

Output:

1
3
5
7
9

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

Output:

0
2
4
6
8

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

Output :

5
7
9

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

Output:

b
d

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

Output:

a
a

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

Output:

2
12

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

Output:

1
3

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!

Statement:
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