📜  Python中的reduce()

📅  最后修改于: 2022-05-13 01:55:13.209000             🧑  作者: Mango

Python中的reduce()

reduce(fun,seq)函数用于将在其参数中传递的特定函数应用于传递的序列中提到的所有列表元素。该函数在“ functools ”模块中定义。

在职的 :

  • 第一步,选择序列的前两个元素并获得结果。
  • 下一步是将相同的函数应用于先前获得的结果和第二个元素之后的数字,然后再次存储结果。
  • 这个过程一直持续到容器中没有更多元素为止。
  • 最终返回的结果被返回并打印在控制台上。
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))


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"]))


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


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


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

输出:

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

使用运算符函数

reduce() 也可以与运算符函数结合使用,以实现与 lambda 函数类似的功能,并使代码更具可读性。

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"]))
输出
The sum of the list elements is : 17
The product of list elements is : 180
The concatenated product is : geeksforgeeks

输出

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

减少()与累积()

reduce() 和accumulate() 都可以用来计算序列元素的总和。但这两者在实现方面存在差异。

  • reduce() 在“functools”模块中定义,accumulate() 在“itertools”模块中定义。
  • reduce() 存储中间结果,只返回最终的求和值。然而,accumulate() 返回一个包含中间结果的迭代器。返回的迭代器的最后一个数字是列表的总和值。
  • reduce(fun,seq) 将函数作为第一个参数,将序列作为第二个参数。相反,accumulate(seq,fun) 将序列作为第一个参数,将函数作为第二个参数。

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))
输出
The summation of list using accumulate is :[1, 4, 8, 18, 22]
The summation of list using reduce is :22

输出:

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

具有三个参数的 reduce()函数

减少函数,即reduce()函数与python3 中的3 个参数以及2 个参数一起工作。简而言之,reduce() 将第三个参数放在第二个参数的值之前(如果存在)。因此,这意味着如果第二个参数是空序列,则第三个参数用作默认参数。

这是一个示例:(此示例取自 functools.reduce() 文档,包括该函数的Python版本:

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
输出
15