ホームページ  >  記事  >  バックエンド開発  >  Python標準ライブラリのitertoolsモジュールの使い方

Python標準ライブラリのitertoolsモジュールの使い方

高洛峰
高洛峰オリジナル
2017-03-21 11:27:541769ブラウズ

はじめに

公式説明: イテレータを作成および使用するための機能ツール。効率的なイテレータを作成するための関数。

itertools.chain(*iterable)

複数のシーケンスを単一のシーケンスとして返します。
例:

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

出力:

i
l
o
v
e
p
y
t
h
o
n

itertools.combinations(iterable, r)

指定された長さの「組み合わせ」を返します
例:

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

出力:

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

itertools.combinations_with_replacement(iterable, r) )

指定された長さの「組み合わせ」を返します。組み合わせ内の要素は繰り返すことができます
例:

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

出力:

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

itertools.product(*iterable[,repeat])

指定された長さのすべての組み合わせを返します。これはデカルト積として理解できます
例:

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])

長さ r の順列を返します
例:

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

出力:

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

itertools.compress(data,selector)

データに対応する要素を返しますセレクターを True に設定します
例:

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

Output :

a
c

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

start から始まりステップを増加させ、無限に増加するシーケンスを返します
例:

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

Output :

1
2
3
.
.

itertools.cycle(iterable)

will イテレーターは無限反復を実行します
例:

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

出力:

a
b
a
b
.
.

itertools.dropwhile(predicate, iterable)

述語が true になるまで、反復可能な後続のデータは返されます。そうでない場合はドロップされます
例:

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

出力:

6
8
2
1

itertools.groupby(iterable[,key])

グループ (key, itera) を返します。key は iterable の値、itera はすべてですkey に等しい項目
例:

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

出力:

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

itertools.ifilter( predicate, iterable)

述語の結果が True である要素反復子を返します。predicate が None の場合、iterable 内の True であるすべての項目を返します。例:

import itertools
for value in itertools.ifilter(lambda x: x % 2, range(10)):
    print value
出力:

1
3
5
7
9
itertools.ifilterfasle(predicate, iterable)

述語が False である要素を返します。 predicate が None の場合、False である反復可能項目をすべて返します

例:

import itertools
for value in itertools.ifilterfalse(lambda x: x % 2, range(10)):
    print value
出力:

0
2
4
6
8
itertools.imap(function,*iterables)

イテレータメソッドmap()と同等

例:

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])

イテレータベースのスライス操作と同等です

例:

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

b
d
itertools .repeat(object,[,times])

times が指定されている場合、オブジェクト object を繰り返し返します。

例:

import itertools
for value in itertools.repeat('a', 2):
    print value
Output:

a
a
itertools.starmap(function, iterable)

Return function(iter)の値、iterはiterableの要素です

例:

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)

predicate が true の場合は、反復可能な要素を返します。 false の場合は、 を返さず、break を返します。

例:

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

1
3

以上がPython標準ライブラリのitertoolsモジュールの使い方の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。