详细分析python3的reduce函数

 更新时间:2017年12月05日 08:40:52   投稿:laozhang  
小编给大家整理了python3的reduce函数详细用法以及相关的技巧,需要的朋友们参考一下吧。

reduce() 函数在 python 2 是内置函数, 从python 3 开始移到了 functools 模块。

官方文档是这样介绍的

reduce(...)
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

从左到右对一个序列的项累计地应用有两个参数的函数,以此合并序列到一个单一值。

例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 计算的就是((((1+2)+3)+4)+5)。

如果提供了 initial 参数,计算时它将被放在序列的所有项前面,如果序列是空的,它也就是计算的默认结果值了

嗯, 这个文档其实不好理解。看了还是不懂。 序列 其实就是python中 tuple list dictionary string 以及其他可迭代物,别的编程语言可能有数组。

reduce 有 三个参数

function 有两个参数的函数, 必需参数
sequence tuple ,list ,dictionary, string等可迭代物,必需参数
initial 初始值, 可选参数

reduce的工作过程是 :在迭代sequence(tuple ,list ,dictionary, string等可迭代物)的过程中,首先把 前两个元素传给 函数参数,函数加工后,然后把得到的结果和第三个元素作为两个参数传给函数参数, 函数加工后得到的结果又和第四个元素作为两个参数传给函数参数,依次类推。 如果传入了 initial 值, 那么首先传的就不是 sequence 的第一个和第二个元素,而是 initial值和 第一个元素。经过这样的累计计算之后合并序列到一个单一返回值

reduce 代码举例,使用REPL演示

>>> def add(x, y):
...  return x+y
...
>>> from functools import reduce
>>> reduce(add, [1,2,3,4])
>>>

上面这段 reduce 代码,其实就相当于 1 + 2 + 3 + 4 = 10, 如果把加号改成乘号, 就成了阶乘了
当然 仅仅是求和的话还有更简单的方法,如下

>>> sum([1,2,3,4])
10
>>>

很多教程只讲了一个加法求和,太简单了,对新手加深理解还不够。下面讲点更深入的例子

还可以把一个整数列表拼成整数,如下

>>> from functools import reduce
>>> reduce(lambda x, y: x * 10 + y, [1 , 2, 3, 4, 5])
12345
>>>

对一个复杂的sequence使用reduce ,看下面代码,更多的代码不再使用REPL, 使用编辑器编写

from functools import reduce
scientists =({'name':'Alan Turing', 'age':105},
    {'name':'Dennis Ritchie', 'age':76},
    {'name':'John von Neumann', 'age':114},
    {'name':'Guido van Rossum', 'age':61})
def reducer(accumulator , value):
 sum = accumulator['age'] + value['age']
 return sum
total_age = reduce(reducer, scientists)
print(total_age)

这段代码会出错,看下图的执行过程

所以代码需要修改

from functools import reduce
scientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},
    {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},
    {'name':'Ada Lovelace', 'age':202, 'gender':'female'},
    {'name':'Frances E. Allen', 'age':84, 'gender':'female'})
def reducer(accumulator , value):
 sum = accumulator + value['age']
 return sum
total_age = reduce(reducer, scientists, 0)
print(total_age)

7, 9 行 红色部分就是修改 部分。 通过 help(reduce) 查看 文档,reduce 有三个参数, 第三个参数是初始值的意思,是可有可无的参数。

修改之后就不出错了,流程如下

这个仍然也可以用 sum 来更简单的完成

sum([x['age'] for x in scientists ])

做点更高级的事情,按性别分组

from functools import reduce
scientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},
    {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},
    {'name':'Ada Lovelace', 'age':202, 'gender':'female'},
    {'name':'Frances E. Allen', 'age':84, 'gender':'female'})
def group_by_gender(accumulator , value):
 accumulator[value['gender']].append(value['name'])
 return accumulator
grouped = reduce(group_by_gender, scientists, {'male':[], 'female':[]})
print(grouped)

输出

{'male': ['Alan Turing', 'Dennis Ritchie'], 'female': ['Ada Lovelace', 'Frances E. Allen']}

可以看到,在 reduce 的初始值参数传入了一个dictionary,, 但是这样写 key 可能出错,还能再进一步自动化,运行时动态插入key

修改代码如下

grouped = reduce(group_by_gender, scientists, collections.defaultdict(list))

当然 先要 import collections 模块

这当然也能用 pythonic way 去解决

import itertools
scientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},
    {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},
    {'name':'Ada Lovelace', 'age':202, 'gender':'female'},
    {'name':'Frances E. Allen', 'age':84, 'gender':'female'})
grouped = {item[0]:list(item[1])
   for item in itertools.groupby(scientists, lambda x: x['gender'])}
print(grouped)

再来一个更晦涩难懂的玩法。工作中要与其他人协作的话,不建议这么用,与上面的例子做同样的事,看不懂无所谓。

from functools import reduce
scientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},
    {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},
    {'name':'Ada Lovelace', 'age':202, 'gender':'female'},
    {'name':'Frances E. Allen', 'age':84, 'gender':'female'})
grouped = reduce(lambda acc, val: {**acc, **{val['gender']: acc[val['gender']]+ [val['name']]}}, scientists, {'male':[], 'female':[]})
print(grouped)

**acc, **{val['gneder']... 这里使用了 dictionary merge syntax , 从 python 3.5 开始引入, 详情请看 PEP 448 - Additional Unpacking Generalizations 怎么使用可以参考这个 python - How to merge two dictionaries in a single expression? - Stack Overflow

python 社区推荐写可读性好的代码,有更好的选择时不建议用reduce,所以 python 2 中内置的reduce 函数 移到了 functools模块中

相关文章

  • 深入了解Python的类与模块化

    深入了解Python的类与模块化

    这篇文章主要介绍了Python中的面向对象编程范式以及模块化思想,并给出相应的实战示例及解释,对我们的学习和工作都有一定的价值,感兴趣的小伙伴可以了解一下
    2021-12-12
  • Python中参数打包和解包的实现

    Python中参数打包和解包的实现

    在Python中,打包和解包参数是一种操作方式,可以将多个参数打包成一个元组或字典,也可以将一个元组或字典解包成多个参数,本文就来介绍一下如何使用
    2023-09-09
  • python通过ssh-powershell监控windows的方法

    python通过ssh-powershell监控windows的方法

    这篇文章主要介绍了python通过ssh-powershell监控windows的方法,涉及Python操作ssh-powershell的相关技巧,需要的朋友可以参考下
    2015-06-06
  • python针对Oracle常见查询操作实例分析

    python针对Oracle常见查询操作实例分析

    这篇文章主要介绍了python针对Oracle常见查询操作,结合实例形式分析了python针对Oracle常见的子查询、多表查询等相关原理、操作技巧与使用注意事项,需要的朋友可以参考下
    2020-04-04
  • python skimage图像处理

    python skimage图像处理

    这篇文章主要为大家介绍了python skimage图像处理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • python实现指定字符串补全空格的方法

    python实现指定字符串补全空格的方法

    这篇文章主要介绍了python实现指定字符串补全空格的方法,涉及Python中rjust,ljust和center方法的使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • Ubuntu配置Pytorch on Graph (PoG)环境过程图解

    Ubuntu配置Pytorch on Graph (PoG)环境过程图解

    这篇文章主要介绍了Ubuntu配置Pytorch on Graph (PoG)环境过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • pandas dataframe拼接后index重新排序方式

    pandas dataframe拼接后index重新排序方式

    这篇文章主要介绍了pandas dataframe拼接后index重新排序方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • Python Excel数据处理之xlrd/xlwt/xlutils模块详解

    Python Excel数据处理之xlrd/xlwt/xlutils模块详解

    在复杂的Excel业务数据处理中,三兄弟扮演的角色缺一不可。如何能够使用xlrd/xlwt/xlutils三个模块来实现数据处理就是今天的内容,希望对大家有所帮助
    2023-03-03
  • python3 读取Excel表格中的数据

    python3 读取Excel表格中的数据

    这篇文章主要介绍了python3 读取Excel表格中的数据的相关资料,需要的朋友可以参考下
    2018-10-10

最新评论