logo

redukovať () v Pythone

The znížiť (zábava, nasledujúca) funkcia sa používa použiť konkrétnu funkciu odovzdanú vo svojom argumente na všetky prvky zoznamu uvedené v postupnosti odovzdanej pozdĺž.Táto funkcia je definovaná v functools modul.

pracuje:

  • V prvom kroku sa vyberú prvé dva prvky sekvencie a získa sa výsledok.
  • Ďalším krokom je použiť rovnakú funkciu na predtým dosiahnutý výsledok a číslo, ktoré nasleduje po druhom prvku a výsledok sa znova uloží.
  • Tento proces pokračuje, kým v nádobe nezostanú žiadne ďalšie prvky.
  • Konečný vrátený výsledok sa vráti a vytlačí na konzole.

Python3




tlačové pole v jazyku 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))>

>

>

Výkon

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

Používanie funkcií operátora

Zníženie() sa môže tiež kombinovať s funkciami operátora, aby sa dosiahla podobná funkcionalita ako pri funkciách lambda a aby bol kód čitateľnejší.

Python3

pivot pandy




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

>

ako previesť celé číslo na reťazec java
>

Výkon

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

znížiť() vs akumulovať()

Na výpočet súčtu prvkov sekvencie je možné použiť redukčné() aj akumulačné(). V oboch sú však rozdiely v implementačných aspektoch.

  • redukcia() je definovaná v module functools, akumulácia() v module itertools.
  • Zníženie() uloží medzivýsledok a vráti iba konečnú hodnotu súčtu. Zatiaľ čo akumulácia () vracia iterátor obsahujúci medzivýsledky. Posledné číslo vráteného iterátora je sumačná hodnota zoznamu.
  • Zníženie (zábava, seq) má funkciu 1. a postupnosť ako 2. argument. Naproti tomu akumulovať (seq, fun) berie sekvenciu ako 1. argument a funkciu ako 2. argument.

Python3




reťazec na celé číslo v jazyku Java
# 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))>

>

>

Výkon

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

redukcia() funkcie s tromi parametrami

Funkcia Reduce, t.j. funkcia Reduce() pracuje s 3 parametrami v python3, ako aj s 2 parametrami. Zjednodušene povedané, reduction() umiestni 3. parameter pred hodnotu druhého, ak je prítomný. To znamená, že ak je 2. argument prázdna postupnosť, potom 3. argument slúži ako predvolený.

Tu je príklad :(Tento príklad bol prevzatý z dokumentáciu functools.reduce(). obsahuje Python verziu funkcie:

Python3




pandy lok

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

>

>

Výkon

15>

Do tohto článku prispeli Manjeet Singh (S. Nandini) .