# 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>>> Išvestis The sum of the list elements is : 17 The maximum element of the list is : 6 Operatoriaus funkcijų naudojimas Reduction() taip pat gali būti derinamas su operatoriaus funkcijomis, kad būtų pasiektas panašus funkcionalumas kaip ir su lambda funkcijomis, o kodas būtų lengviau skaitomas. 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'> ]))> | Išvestis The sum of the list elements is : 17 The product of list elements is : 180 The concatenated product is : geeksforgeeks sumažinti () vs kaupti () Skaičiuojant sekos elementų sumavimą, galima naudoti ir redukuoti() ir kaupti(). Tačiau abiejuose įgyvendinimo aspektuose yra skirtumų. - Reduction() apibrėžiamas functools modulyje, akumuliuoja() itertools modulyje.
- Reduction() išsaugo tarpinį rezultatą ir grąžina tik galutinę sumavimo reikšmę. Tuo tarpu „akumuli“ () grąžina iteratorių, kuriame yra tarpiniai rezultatai. Paskutinis grąžinto iteratoriaus skaičius yra sąrašo suminė vertė.
- Reduction(fun, seq) naudoja funkciją kaip 1-ą, o seką – kaip 2-ąjį argumentą. Priešingai, kaupimas (seq, fun) seką laiko 1-uoju argumentu, o funkciją – 2-uoju argumentu.
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))> | Išvestis The summation of list using accumulate is :[1, 4, 8, 18, 22] The summation of list using reduce is :22 redukcijos () funkcija su trimis parametrais Sumažinimo funkcija, ty redukcijos () funkcija veikia su 3 parametrais python3, taip pat su 2 parametrais. Paprasčiau tariant, reduction() įdeda trečiąjį parametrą prieš antrojo reikšmę, jei jis yra. Taigi, tai reiškia, kad jei 2-asis argumentas yra tuščia seka, tada 3-asis argumentas naudojamas kaip numatytasis. Štai pavyzdys: (Šis pavyzdys paimtas iš functools.reduce() dokumentaciją apima Python funkcijos versiją: 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> | Išvestis 15 Prie šio straipsnio prisidėjo Manjeet Singh (S. Nandini) .
Top Straipsniai
Kategorija
|