logo

reduce() v Pythonu

The zmanjšaj (zabavno, zaporedje) funkcija se uporablja za uporabi določeno funkcijo, posredovano v njenem argumentu, za vse elemente seznama omenjeno v posredovanem zaporedju. Ta funkcija je definirana v functools modul.

Delo:

  • V prvem koraku izberemo prva dva elementa zaporedja in dobimo rezultat.
  • Naslednji korak je uporaba iste funkcije za predhodno dosežen rezultat in število, ki sledi drugemu elementu, rezultat pa se ponovno shrani.
  • Ta postopek se nadaljuje, dokler v vsebniku ne ostane noben element.
  • Končni vrnjeni rezultat se vrne in natisne na konzoli.

Python3



obračanje niza java




# 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>else> b, lis))>

>

>

Izhod

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

Uporaba funkcij operaterja

reduce() lahko kombinirate tudi z operatorskimi funkcijami, da dosežete podobno funkcionalnost kot z lambda funkcijami in naredite kodo bolj berljivo.

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

>

>

Izhod

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

zmanjšaj() proti nakopiči()

Za izračun vsote elementov zaporedja se lahko uporabita tako reduce() kot collecte(). Vendar obstajajo razlike v vidikih izvajanja v obeh.

  • reduce() je definiran v modulu functools, collecte() v modulu itertools.
  • reduce() shrani vmesni rezultat in vrne samo končno vrednost seštevka. Medtem ko akumulacija() vrne iterator, ki vsebuje vmesne rezultate. Zadnja vrnjena številka iteratorja je vrednost seštevka seznama.
  • reduce(fun, seq) sprejme funkcijo kot 1. in zaporedje kot 2. argument. V nasprotju s tem akumulacija(seq, fun) vzame zaporedje kot 1. argument in funkcijo kot 2. argument.

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

operatorji v programiranju python

>

>

Izhod

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

funkcijo reduce() s tremi parametri

Funkcija reduciranja, tj. funkcija reduciranja() deluje s 3 parametri v python3 kot tudi z 2 parametroma. Povedano na preprost način, reduce() postavi 3. parameter pred vrednost drugega, če je prisoten. To torej pomeni, da če je 2. argument prazno zaporedje, potem 3. argument služi kot privzeti.

Tukaj je primer :(Ta primer je bil vzet iz dokumentacija functools.reduce(). vključuje različico funkcije Python:

java za vrste zank

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>

>

>

Izhod

15>

Ta članek je prispeval Manjeet Singh (S. Nandini) .