snížit() v Pythonu
The snížit (zábava, další) funkce je zvyklá aplikovat konkrétní funkci předanou ve svém argumentu na všechny prvky seznamu uvedená v předávané sekvenci. Tato funkce je definována v functools modul.
Pracovní :
- V prvním kroku jsou vybrány první dva prvky sekvence a je získán výsledek.
- Dalším krokem je aplikování stejné funkce na dříve dosažený výsledek a číslo těsně po druhém prvku a výsledek je znovu uložen.
- Tento proces pokračuje, dokud v kontejneru nezůstanou žádné další prvky.
- Konečný vrácený výsledek je vrácen a vytištěn na konzole.
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> else> b, lis))> |
Výstup
The sum of the list elements is : 17 The maximum element of the list is : 6
Použití funkcí operátora
redukci() lze také kombinovat s operátorskými funkcemi, aby se dosáhlo podobné funkčnosti jako u funkcí lambda a kód byl čitelnější.
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'> ]))> |
Výstup
The sum of the list elements is : 17 The product of list elements is : 180 The concatenated product is : geeksforgeeks
snížit() vs akumulovat()
K výpočtu součtu prvků sekvence lze použít funkce reduction() i akumulovat(). Ale existují rozdíly v aspektech implementace v obou těchto.
- reduction() je definováno v modulu functools, akumulovat() v modulu itertools.
- reduction() ukládá mezivýsledek a vrací pouze konečnou hodnotu součtu. Zatímco akumulovat() vrací iterátor obsahující mezivýsledky. Poslední číslo vráceného iterátoru je sumační hodnota seznamu.
- reduction(fun, seq) má funkci jako 1. a sekvence jako 2. argument. Naproti tomu akumulovat (seq, fun) bere sekvenci jako 1. argument a funkci jako 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))> |
Výstup
The summation of list using accumulate is :[1, 4, 8, 18, 22] The summation of list using reduce is :22
funkce reduction() se třemi parametry
Funkce Reduce, tj. funkce Reduce() pracuje se 3 parametry v python3 a také se 2 parametry. Jednoduše řečeno, redukovat() umístí 3. parametr před hodnotu druhého, pokud je přítomen. To znamená, že pokud je 2. argument prázdná sekvence, pak 3. argument slouží jako výchozí.
Zde je příklad :(Tento příklad byl převzat z dokumentaci functools.reduce(). obsahuje Python verzi funkce:
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> |
Výstup
15
Do tohoto článku přispěl Manjeet Singh (S. Nandini) .