Pythonのreduce()

リデュース(楽しい,シーケンス) 関数が使用されるのは 引数に渡された特定の関数をリストのすべての要素に適用します。 渡されたシーケンスで言及されています。この関数はで定義されています 関数ツール モジュール。

働く :

  • 最初のステップでは、シーケンスの最初の 2 つの要素が選択され、結果が取得されます。
  • 次のステップでは、以前に取得した結果と 2 番目の要素の直後にある数値に同じ関数を適用し、結果が再度保存されます。
  • このプロセスは、コンテナー内の要素がなくなるまで続きます。
  • 最終的に返された結果が返され、コンソールに表示されます。

Python3




# python code to demonstrate working of reduce()> > # importing functools for reduce()> import> functools> > # initializing list> lis> => [> 1> ,> 3> ,> 5> ,> 6> ,> 2> ]> > # using reduce to compute sum of list> print> (> 'The sum of the list elements is : '> , end> => '')> print> (functools.> reduce> (> lambda> a, b: a> +> b, lis))> > # using reduce to compute maximum element from list> print> (> 'The maximum element of the list is : '> , end> => '')> print> (functools.> reduce> (> lambda> a, b: a> if> a>b>>

出力

The sum of the list elements is : 17 The maximum element of the list is : 6 

演算子関数の使用

また、reduce() を演算子関数と組み合わせてラムダ関数と同様の機能を実現し、コードを読みやすくすることもできます。

Python3




# python code to demonstrate working of reduce()> # using operator functions> > # importing functools for reduce()> import> functools> > # importing operator for operator functions> import> operator> > # initializing list> lis> => [> 1> ,> 3> ,> 5> ,> 6> ,> 2> ]> > # using reduce to compute sum of list> # using operator functions> print> (> 'The sum of the list elements is : '> , end> => '')> print> (functools.> reduce> (operator.add, lis))> > # using reduce to compute product> # using operator functions> print> (> 'The product of list elements is : '> , end> => '')> print> (functools.> reduce> (operator.mul, lis))> > # using reduce to concatenate string> print> (> 'The concatenated product is : '> , end> => '')> print> (functools.> reduce> (operator.add, [> 'geeks'> ,> 'for'> ,> 'geeks'> ]))>

出力

The sum of the list elements is : 17 The product of list elements is : 180 The concatenated product is : geeksforgeeks 

reduce() とaccumulate()

reduce() とaccumulate() の両方を使用して、シーケンス要素の合計を計算できます。ただし、これらの両方には実装の側面に違いがあります。

  • reduce() は functools モジュールで定義され、accumulate() は itertools モジュールで定義されます。
  • reduce() は中間結果を保存し、最終的な合計値のみを返します。一方、accumulate() は中間結果を含むイテレータを返します。返されるイテレータの最後の番号はリストの合計値です。
  • reduce(fun, seq) は関数を 1 番目の引数として、シーケンスを 2 番目の引数として受け取ります。対照的に、accumulate(seq, fun) はシーケンスを 1 番目の引数として、関数を 2 番目の引数として受け取ります。

Python3




# python code to demonstrate summation> # using reduce() and accumulate()> > # importing itertools for accumulate()> import> itertools> > # importing functools for reduce()> import> functools> > # initializing list> lis> => [> 1> ,> 3> ,> 4> ,> 10> ,> 4> ]> > # printing summation using accumulate()> print> (> 'The summation of list using accumulate is :'> , end> => '')> print> (> list> (itertools.accumulate(lis,> lambda> x, y: x> +> y)))> > # printing summation using reduce()> print> (> 'The summation of list using reduce is :'> , end> => '')> print> (functools.> reduce> (> lambda> x, y: x> +> y, lis))>

出力

The summation of list using accumulate is :[1, 4, 8, 18, 22] The summation of list using reduce is :22 

3 つのパラメータを持つreduce()関数

Reduce関数、つまりreduce()関数は、Python3の3つのパラメータだけでなく2つのパラメータでも機能します。簡単に言うと、reduce() は、3 番目のパラメータが存在する場合、2 番目のパラメータの値の前に配置します。したがって、2 番目の引数が空のシーケンスの場合、3 番目の引数がデフォルトの引数として機能することを意味します。

以下に例を示します:(この例は、 functools.reduce() ドキュメント 関数の Python バージョンが含まれています。

Python3




# Python program to illustrate sum of two numbers.> def> reduce> (function, iterable, initializer> => None> ):> > it> => iter> (iterable)> > if> initializer> is> None> :> > value> => next> (it)> > else> :> > value> => initializer> > for> element> in> it:> > value> => function(value, element)> > return> value> > # Note that the initializer, when not None, is used as the first value instead of the first value from iterable , and after the whole iterable.> tup> => (> 2> ,> 1> ,> 0> ,> 2> ,> 2> ,> 0> ,> 0> ,> 2> )> print> (> reduce> (> lambda> x, y: x> +> y, tup,> 6> ))> > # This code is contributed by aashutoshjha>

出力

15 

この記事は次の寄稿者です マンジート・シン(S.ナンディーニ)