# 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>>> Izvade The sum of the list elements is : 17 The maximum element of the list is : 6 Operatora funkciju izmantošana Reduction() var arī kombinēt ar operatora funkcijām, lai sasniegtu līdzīgu funkcionalitāti kā ar lambda funkcijām un padarītu kodu lasāmāku. 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'> ]))> | Izvade The sum of the list elements is : 17 The product of list elements is : 180 The concatenated product is : geeksforgeeks samazināt () vs uzkrāt () Secības elementu summēšanas aprēķināšanai var izmantot gan reducēt(), gan uzkrāt(). Taču īstenošanas aspektos abos gadījumos ir atšķirības. - Reduction() ir definēts functools modulī, akumul() itertools modulī.
- Reduction() saglabā starprezultātu un atgriež tikai galīgo summēšanas vērtību. Tā kā akumulators () atgriež iteratoru, kas satur starprezultātus. Pēdējais atgrieztā iteratora numurs ir saraksta summēšanas vērtība.
- Reduction(fun, seq) izmanto funkciju kā 1. un secību kā 2. argumentu. Turpretim uzkrāšana (seq, fun) izmanto secību kā 1. argumentu un funkciju kā 2. 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))> | Izvade The summation of list using accumulate is :[1, 4, 8, 18, 22] The summation of list using reduce is :22 samazināt() funkciju ar trim parametriem Samazināšanas funkcija, t.i., funkcija samazināt () darbojas ar 3 parametriem python3, kā arī ar 2 parametriem. Vienkāršāk sakot, reduction() novieto 3. parametru pirms otrā vērtības, ja tāds ir. Tādējādi tas nozīmē, ka, ja 2. arguments ir tukša secība, tad 3. arguments kalpo kā noklusējuma arguments. Šeit ir piemērs: (Šis piemērs ir ņemts no functools.reduce() dokumentāciju ietver funkcijas Python versiju: 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> | Izvade 15 Šī raksta autors ir Mandžs Sings (S. Nandini) .
|