python 3.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce.

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.

意思就是对sequence连续使用function, 如果不给出initial, 则第一次调用传递sequence的两个元素, 以后把前一次调用的结果和sequence的下一个元素传递给function. 如果给出initial, 则第一次传递initial和sequence的第一个元素给function

*functool标准库还有很多功能,可以参考网上的资料

转载于:https://www.cnblogs.com/apollo1616/p/9527744.html

Python3 不能直接导入reduce相关推荐

  1. python命令窗口中怎么导入numpy_科学网—windows7下python3.6如何导入numpy,Astropy - 杨涛只的博文...

    本人虽然安装了Python3.6好久,但是因为当时一直没有成功导入numpy,故而无法正常使用,今日幸得祥云师弟帮助,给导入了numpy.具体如下, 1,首先找到安装的路径,由于刚开始安装的时候,不知 ...

  2. Python3 map函数与reduce函数

    # -*- coding:utf-8 -*- # 传入一个数字,返回这个数字的平方 def calc(x): s = x * xreturn slist1 = [1,2,3,4,5,6,7,8,9,1 ...

  3. 【python】 针对python3 下无法导入tkinter

    正文之前 我的一个python GUI不知道发哪门子疯,在mac下面一直杀不死进程,还提示我说啥没有这个模块!我丢? hustwolf@hustwolf-master:~/Python$ python ...

  4. python3精要(29)-reduce

    reduce接受迭代器进行反复迭代,并返回单一结果 from functools import reduce x=range(1,20) print(reduce((lambda x,y:x+y),x ...

  5. python如何导入numpy简书_如何使用python3.x成功导入numpy?

    现在,来讨论这个问题.我成功地安装了numpy 1.7.0,虽然我必须先做一些注册表版本,但是在尝试将其导入shell时,我遇到了大量错误:Traceback (most recent call la ...

  6. python3中reduce函数的使用

    在python3中如果使用reduce需要先导入 from functools import reduce reduce函数,reduce函数会对参数序列中元素进行累积. reduce函数的定义: r ...

  7. python3导入_Python3导入相对还是绝对的正确方法?

    我正在编写一个python模块.在Python2中一切正常,但在Python3中,导入失败. 这是我的代码结构.neuralnet/ __init__.py train.py # A wrapper ...

  8. python高阶函数、map reduce 自己如何去定义_「python」高阶函数map、reduce的介绍

    Python map 先看官方介绍: map(function, iterable, ...)Return an iterator that applies function to every ite ...

  9. reduce python3_python3中reduce函数的使用

    在python3中如果使用reduce需要先导入 from functools import reduce reduce函数,reduce函数会对参数序列中元素进行累积. reduce函数的定义: r ...

最新文章

  1. libcudart.so.6.5 cannot open shared object file: no such file or directory
  2. JavaScript初学者编程题(23)
  3. 不存在_MySQL数据存在就更新,不存在就添加
  4. cgdb基本用法总结
  5. mysql 5.7 windows 主从_windows server下Mysql5.7主从复制
  6. MATLAB GPU编程基础
  7. C语言 判断两个字符串大小相等关系
  8. 堆排序(python实现)
  9. win7开机动画怎么换
  10. 魅族e升级android,魅族魅蓝E升级教程_魅族魅蓝E的OTA系统升级方法
  11. 8uftp,8uftp绿色版软件有哪些功能
  12. Markdown基本语法介绍及cmd markdown下载
  13. 带图像识别的YYS连点器 V2.0
  14. 第五章 数组及排序 ① 笔记
  15. python数据挖掘母亲和孩子身高预测_孩子身高预测公式 靠谱指数高达80分哦!
  16. 微信如何群删好友 微信群删好友的方法教程
  17. Spring容器是什么?为什么要使用Spring容器?
  18. Spire操作word文档
  19. php00截断原理,burpsuite上传截断及截断原理介绍
  20. 建立一个网站的具体过程

热门文章

  1. 计算机硬件课题,计算机硬件技术基础免试课题.pptx
  2. 如何用python画长方形_python opencv 画矩形跟老齐学Python之用Python计算
  3. 浙大计算机科学与技术FPGA,Computer Science 计算机科学与技术系 重要国际学术会议...
  4. Spark数据统计指标计算
  5. R︱mlr包挑选最适机器学习模型+变量评估与选择(案例详解)
  6. R语言︱文本挖掘套餐包之——XML+SnowballC+tm包
  7. [Flink]Flink DataStream API 概览
  8. oracle通过闪回查询表的更改记录
  9. 「docker实战篇」python的docker爬虫技术-在linux下mitmproxy介绍和安装(四)
  10. 数据库版本管理工具Flyway应用