from functools import reduse    从模块中导入

reduce函数: 处理一个序列,然后把序列进行合并操作

#**** 问题:求1+2+3+100的和是多少?
# 一,原始方法:
num_1 = [1,2,3,100]
res = 0
for i in num_1:
res += i
print('结果:',res)
结果: 106 ## 二,固定写死方法:
num_1 = [1,2,3,100]
def test(array):
res = 0
for i in array:
res += i
return res
print('结果:',test(num_1))
结果: 106 ## 三,灵活写法: 加法变成乘法
num_1 = [1,2,3,100]
# def qsum(x,y):
# return x+y
def test(func,array):
res = array.pop(0)
for i in array:
res += i
return res
print('结果:',test(lambda x,y:x+y,num_1))
结果: 106 num_2 = [1,8,3,100]
# def mulit(x,y):
# return x*y
def test(func,array):
res = array.pop(0)
for i in array:
res = func(res,i)
return res
print('结果:',test(lambda x,y:x*y,num_2))
结果: 2400

reduce 函数用法:

## 终极版
from functools import reduce
num_1 = [1,2,3,100]
print(reduce(lambda x,y:x+y,num_1))
print(reduce(lambda x,y:x+y,num_1,60)) 106
166

py-day4-1 python reduce函数的更多相关文章

  1. python reduce()函数

    reduce()函数 reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传 ...

  2. 弄明白python reduce 函数

    作者:Panda Fang 出处:http://www.cnblogs.com/lonkiss/p/understanding-python-reduce-function.html 原创文章,转载请 ...

  3. python的reduce函数的使用方法详解以及使用案例,相加,相乘(处理一个序列,然后把序列进程合并操作)

    1.求列表的数字相加之和,还是之前的习惯,写for循环来实现 num_1=[1,2,3,4,5,6,7,8,9] a=0 for n in num_1: #a=a+n a+=n print (a) C ...

  4. 018.Python迭代器以及map和reduce函数

    一 迭代器 能被next进行调用,并且不断返回下一个值的对象 特征:迭代器会生成惰性序列,它通过计算把值依次的返回,一边循环一边计算而不是一次性得到所有数据 优点:需要数据的时候,一次取一个,可以大大 ...

  5. Python的map、filter、reduce函数 [转]

    1. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq): mapped_seq = []        fo ...

  6. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下   Py ...

  7. python的reduce()函数

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  8. Python lambda和reduce函数

    看到一篇博文写lambda和reduce函数.笔者小痒了一下,用Python实现一下: #! /usr/bin/env python # -*-coding:utf-8-*- import time ...

  9. Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...

随机推荐

  1. Python基础学习---位运算符

    <<   左移,每移动1位,相当于乘以2      例如:32<<2    等价于:32*4 ==128 >>   右移,每移动1位,相当于除以2      例如: ...

  2. 推理机Jess,Racer,Jena

    推理机 Jess(Java Expert Shell System)是基于Java语言的CLISP推理机. CLISP是基于产生式的前向推理引擎,许多上层的推理任务,都要映射到这个推理引擎上来运行. ...

  3. GitHub入门与实践 读书笔记三:(1)GitHub账户注册教程

    第一步:进入GitHub官网,官网地址:https://github.com/ 第二步:点击Sign up for GitHub 1.昵称一栏:每次在你输入昵称之后,都会检查是否已经被注册.如果被注册 ...

  4. 利用div+css实现九宫格,然后用js实现点击每个格子可以随机更改格子(div)的背景颜色

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. pycharm中字体大小的调整方法

    一.file->settings->editor->font->size 二.file中键入mouse,在其下editor->general->mouse选中:ch ...

  6. Breadth-first search

    given a graph G  and a distinguished source vertex s, breadth-firstsearch systematically explores th ...

  7. shell 删除颜色代码

    sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

  8. TFS2015安装、配置

    一.安装,参考:http://www.cnblogs.com/WilsonWu/archive/2011/11/24/2261674.html 二.配置,参考:http://www.cnblogs.c ...

  9. Gym101889J. Jumping frog(合数分解+环形dp预处理)

    比赛链接:传送门 题目大意: 一只青蛙在长度为N的字符串上跳跃,“R”可以跳上去,“P”不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1, N-1],选定K之后不可改 ...

  10. VMware Ubuntu安装过程

    一.下载Ubuntu镜像文件 1.到官网下载:http://www.ubuntu.com 2.百度云(16.4.6): 链接:https://pan.baidu.com/s/14IlVP--D5mtZ ...