그만큼 감소(재미,순서) 기능은 다음과 같은 데 사용됩니다. 인수에 전달된 특정 함수를 모든 목록 요소에 적용합니다. 전달된 시퀀스에서 언급되었습니다. 이 함수는 다음에 정의되어 있습니다. 기능 도구 기준 치수.
일하고 있는 :
- 첫 번째 단계에서는 시퀀스의 처음 두 요소를 선택하고 결과를 얻습니다.
- 다음 단계는 이전에 얻은 결과와 두 번째 요소 바로 다음의 숫자에 동일한 기능을 적용하고 결과가 다시 저장되는 것입니다.
- 이 프로세스는 컨테이너에 더 이상 요소가 남지 않을 때까지 계속됩니다.
- 최종 반환 결과가 반환되어 콘솔에 인쇄됩니다.
파이썬3
자바 if문
# 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>비> else> b, lis))> |
>
>산출
The sum of the list elements is : 17 The maximum element of the list is : 6>
연산자 기능 사용
Reduce()는 연산자 함수와 결합하여 람다 함수와 유사한 기능을 달성하고 코드를 더 읽기 쉽게 만들 수도 있습니다.
파이썬3
# 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'> ]))> |
>
>산출
The sum of the list elements is : 17 The product of list elements is : 180 The concatenated product is : geeksforgeeks>
감소() 대 축적()
감소()와 누적()은 모두 시퀀스 요소의 합을 계산하는 데 사용할 수 있습니다. 그러나 두 가지 모두 구현 측면에 차이가 있습니다.
- Reduce()는 functools 모듈에 정의되어 있고, Accumulate()는 itertools 모듈에 정의되어 있습니다.
- Reduce()는 중간 결과를 저장하고 최종 합계 값만 반환합니다. 반면, stack()은 중간 결과를 포함하는 반복자를 반환합니다. 반환된 반복자의 마지막 숫자는 목록의 합계 값입니다.
- Reduce(fun, seq)는 첫 번째 인수로 함수를, 두 번째 인수로 시퀀스를 사용합니다. 대조적으로 누적(seq, fun)은 시퀀스를 첫 번째 인수로, 함수를 두 번째 인수로 사용합니다.
파이썬3
# 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))> |
>
>산출
The summation of list using accumulate is :[1, 4, 8, 18, 22] The summation of list using reduce is :22>
세 개의 매개변수를 갖는 Reduce() 함수
Reduce 함수, 즉 Reduce() 함수는 python3의 3개 매개변수와 2개의 매개변수에 대해 작동합니다. 간단히 말해서, Reduce()는 세 번째 매개변수를 두 번째 매개변수 값 앞에 배치합니다(있는 경우). 따라서 두 번째 인수가 빈 시퀀스인 경우 세 번째 인수가 기본 인수로 사용됩니다.
다음은 예입니다.(이 예는 functools.reduce() 문서 Python 버전의 함수가 포함되어 있습니다.
파이썬3
jframe
# 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> |
>
>산출
15>
이 기사는 기고자: 만지트 싱(S. Nandini) .