Python Itertools-効率的なループ処理のためのイテレータを作成する関数です。

Python Itertoolsです。
このモジュールは、単体でも組み合わせても便利な、高速でメモリ効率の良いツールのコアセットを標準化しています。
Python itertoolsモジュールはPython Standard Libraryで提供されており、Pythonに特化したツールを簡潔かつ効率的に構築することができます。
このモジュールが提供するイテレータの種類:
- 無限イテレータ
- 最短の入力シーケンスで終了するイテレータ
- combinatoricイテレータ
戻り値のタイプはイテレータオブジェクトになります。
- next()関数を使う
- forループを使う
- list()を使ってリストオブジェクトに変換する
無限のイテレータ。
count(),cycle(),repeat()
- count():
引数 start で指定した数から始まる等間隔の値を返すイテレータを作成します。デフォルトでは、startは0、stepは1です。
itertools.count(start=0,step=1)
map()やzip()関数の引数としても使用されています。
zip()
-zip()関数はPythonの組み込み関数で、複数の配列から対応する要素を結合し、タプルのイテレータであるzipオブジェクトを返すことができます。 itertools.count()
は、zip()関数のシーケンスとして使用できます。
map()
-Pythonのmap()関数は、指定されたiterableに含まれる全ての要素に関数を適用し、イテレータであるmapオブジェクトを返します。 map()関数の引数として、itertools.count()を使用することができます。
2.cycle():
イテレートから要素を返し、それぞれのコピーを保存するイテレータを作成します。
itertools.cycle(iterable)
3.repeat():
何度もオブジェクトを返すイテレータを作成します。引数の回数が指定されない限り無限に実行されます。
map()やzip()関数の引数として使用されます。
itertools.repeat(object,times)
map()関数の引数として使われています。
zip()関数の引数として使われています。
最短の入力配列で終了するイテレータ:
- accumulate():
累積した和や、func-parameterで指定した他の二項関数の累積結果を返すイテレータを作成します。
通常、出力イテレートテーブルの要素数は入力イテレートテーブルの要素数と一致しますが、initial-parameterが指定されている場合は累積されるので、出力イテレートテーブルには入力イテレートテーブルよりも1要素多く含まれます。functools.reduce()
は、同様の関数で最終的な累積値のみを返します。
初期パラメータはPython version 3.8
itertools.accumulate(iterable,func,*,initial=None)
2. chain():
最初のイテレータから要素を返し、それがなくなるまで次のイテレータに進むイテレータを作成します。 itertools.chain(*iterables)
3. chain.from_iterable():
この関数は、1つのイテレートを入力引数として受け取り、入力イテレート内のすべての要素を含むフラット化されたイテレートを返します。
chain.from_iterable(iterable)
4. compress():
データから要素をフィルタリングして、selectorsで対応する要素がTrueと評価されたものだけを返すイテレータを作ります。
itertools.compress(data,selectors)
5.dropwhile():
述語がTrueである限りイテレートから要素を落とし、その後すべての要素を返すイテレータを作ります。
itertools.dropwhile(predicate,iterable)
6.takewhile():
述語がTrueである限り、反復可能な要素を返すイテレータを作成します。
itertools.takewhile(predicate,iterable)
7.filterfalse():
predicateがFalseのものだけを返すようにiterableから要素をフィルタリングするイテレータを作ります。filter()
メソッドは、関数が True を返すイテレートの要素を含むイテレータを返します。
itertools.filterfalse(predicate,iterables)
8.zip_longest():
各iterableの要素を集約したイテレータを作成します。 反復子の長さが不揃いの場合、足りない値はフィルバリューで埋められます。
ZIP()では、最も短いイテレータがなくなるまで反復を続けます。
itertools.zip_longest(*iterables,fillvalue=None)
9.starmap():
イテレートから得られた引数を使って関数を計算するイテレータを作ります。 map()の代わりに使うのは、引数のパラメータがすでに1つのiterableからのタプルでグループ化されている場合です(データは “pre-zipped “されています)。
itertools.starmap(function,iterable)
10. islice():
イテレートから選択された要素を返すイテレータを作成します。 startがNone
であれば、イテレートは0から始まります。 stepがNone
の場合、ステップはデフォルトで1になります。stopがNone
islice()
は、start、stop、stepに負の値をサポートしていません。
itertools.islice(iterable,stop)
itertools.islice(iterable, start, stop)
11.tee():
1つのイテレートからn個の独立したイテレータを返します。
itertools.tee(iterable,n=2)
12.groupby():
イテレートから連続したキーとグループを返すイテレータを作ります。
key は各要素のキー値を計算する関数です。key が指定されない場合や None の場合、key はデフォルトで ID 関数となり、要素は変更されずに返されます。
itertools.groupby(iterable,key=None)
コンビナトリアルイテレータ:
- product():
入力イテレートのカルテシアン積。
カルテジアン積の定義。 集合Xと集合Yの積は、xがXに属し、yがYに属するすべての順序付きペア( x, y )を含む集合である。
反復可能なものとそれ自身との積を計算するには、オプションのrepeatキーワード引数で繰り返しの数を指定します。
itertools.product(*iterables,repeat)
2. permutations():
イテレート可能な要素の連続した r
長さの順列を返します。 rが指定されていないかNoneの場合、rは反復可能な要素の長さにデフォルトで設定され、すべての可能な全長順列が生成されます。順列タプルは、入力反復可能な要素の順序に従って辞書式順序で出力されます。
要素は、その値ではなく、位置に基づいて一意に扱われます。
itertools.permutations(iterable,r=None)
注意:並べ替えでは、要素の順序が重要です。
3. combinations():
Return r
入力されたiterableから要素の長さの部分列を返します。
組み合わせのタプルは、入力されたiterableの順序に従って辞書式順序で出力されます。
辞書式順序とは、単語を構成する文字のアルファベット順に並べる方法を意味します。
要素は、その値ではなく、位置に基づいて一意に扱われます。
itertools.combinations(iterable, r)
4. combinations_with_replacement():
r
入力された反復可能な要素のうち、個々の要素を複数回繰り返すことができる長さのサブシーケンスを返します。
itertools.combinations_with_replacement(iterable, r)
Note:
1. Used as an argument in map() and zip():
count(),repeat()
repeat()- supply stream of constant values to map() or zip() function.
count()-it will supply different values to map() or zip() function.2.Difference between cycle() and repeat():
cycle()-iterates over the same object again and again
repeat()-returns the same object again and again.3.Difference between reduce() and itertools.accumulate():
reduce():
* It will return only the final accumulated value.
* First argument should be function and second argument should be iterable.
accumulate()
* It will return the running accumulated value.The elements in the output iterable will be equal to elements in the input iterable,if initial value is not mentioned.
* First argument should be iterable and second argument should be function.4.Difference between filter() and itertools.compress():
* filter()function-filters the given iterable with the help of function that test each element in the iterable is True or not.
* compress():filters the given iterable based on the corresponding element in the selector. Iterable containing True /False is given as selector.5.Difference between filter() and itertools.filterfalse():
*filter():Construct an iterator from the elements of iterable for which function returns True.
*filterfalse():Construct an iterator from the elements of iterable for which function returns False.6.Difference between zip() and itertools.zip_longest():
*zip():Iteration continues until shortest iterable is exhausted.
*zip_longest():Iteration continues until longest iterable is exhausted.7.Difference between list slicing and itertools.islice():
*List slicing creates new list
*islice()-Returns an iterator.We can loop through the iterator,in the way we want.
8.Difference between itertools.permutations() and itertools.combinations():
* itertools.permutations():Order of the elements does matters.
*itertools.combinations():Order of the elements doesn’t matters.
Both combinations and permutations doesn’t repeat values.
9.Difference between itertools.combinations() and itertools.combinations_with_replacement
* combinations():Order of element doesn’t matters and doesn’t repeat values.
* combinations_with_replacement():Order of element doesn’t matters and it repeats values.10.Difference between itertools.takewhile() and itertools.dropwhile():
* takewhile():Make an iterator that returns element from the iterable as long as the predicate is True.
* dropwhile():Make an iterator that drops element from the iterable as long as the predicate is True afterwards returns every element.
となります。