美文网首页
functool.reduce()函数 Python

functool.reduce()函数 Python

作者: RayRaymond | 来源:发表于2020-04-30 09:25 被阅读0次

对将一个可迭代数据集合中的所有数据进行累积。

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() 原理图
from functools import reduce


reduce(function, iterable[, initializer])
  • function 函数,两个参数
  • sequence 可迭代的 - tuple/list/dict/str
  • initial 可选参数。初始值。如果有initial,计算时初始值放在所有项前面
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from functools import reduce
>>>
>>> def add(x,y):
...     return x+y
...
>>> reduce(add,[1,2,3,4])
10
  • 使用 lambda
# 累加
>>> reduce(lambda x,y:x+y, [1,2,3,4])
10

# 把列表中数按位拼成整数
>>> reduce(lambda x,y: x*10+y, [1,2,3])
123


  • 增加初始值
>>> reduce(lambda x,y:x+y, [1,2,3,4], 1)
11

# 逆序字符串
>>> reduce(lambda x,y:y+x, 'abcdefg')
'gfedcba'
>>> reduce(lambda x,y:y+x, 'abcdefg','xyz')
'gfedcbaxyz'

  • 进阶
from functools import reduce
import collections


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, collections.defaultdict(list))

print(grouped)

等同于 groupby

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)

相关文章

  • functool.reduce()函数 Python

    对将一个可迭代数据集合中的所有数据进行累积。 Apply a function of two arguments ...

  • Python - 2017/01/28-函数

    调用python内置函数 函数名(参数) 即可调用python内置函数 help(函数名) 返回python对于函...

  • Python函数式介绍一 - 高阶函数

    Python函数式介绍一 - 高阶函数Python函数式介绍二 - 链式调用 最近为了给朋友推广Python函数式...

  • Python高阶函数学习笔记

    python中的高阶函数是指能够接收函数作为参数的函数 python中map()函数map()是 Python 内...

  • Python学习笔记1

    Python注释 Python变量 Python运算符 Python输入输出 输入函数 输出函数(3.x) ...

  • Python:内置函数

    python的内置函数,匿名函数 内置函数 内置函数就是python给你提供的,拿来直接用的函数,比如print,...

  • 二级Python----Python的内置函数及标准库(DAY

    Python的内置函数 嵌入到主调函数中的函数称为内置函数,又称内嵌函数。 python的内置函数(68个) Py...

  • python3 range() 函数和 xrange() 函数

    python3 range 函数 python3 取消了 xrange() 函数,并且和 range() 函数合并...

  • 7、函数

    1、Python之什么是函数 2、Python之调用函数 Python内置了很多有用的函数,我们可以直接调用。 要...

  • Python入门

    Python3教程 安装Python 第一个Python程序 Python基础 函数 高级特性 函数式编程 模块 ...

网友评论

      本文标题:functool.reduce()函数 Python

      本文链接:https://www.haomeiwen.com/subject/smiawhtx.html