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. vue跳坑笔记

    序号 报错截图 关键词 解决办法 1 - code EPERM errno 4048 syscall scandir operation not permitted 清除npm缓存,命令: npm c ...

  2. L358 World Book Day

    World Book Day is celebrated by UNESCO and other related organisations every year on the 23rd of Apr ...

  3. beifen

    Comparison of Models for Predicting the Outcome of Craniocerebral Injury by Using Machine Learning   ...

  4. Real time profiler for Delphi applications

    xalion提供的资源,这么强,还是免费的,快去试用!   ✓  Detailed debug information (internal, TDS, MAP) ✓  Display informat ...

  5. html css样式子元素相对父级元素定位

    废话不多说. 父级元素 样式设置: position:relative; 子元素样式: position: absolute; 这样就可以达到子元素相对父级元素定位了.

  6. 同一个服务器部署两个Tomcat并用Nginx实现反向代理

    需求场景:由于服务器只有80端口可供外网访问,但需要部署两台tomcat来运行两个不同的项目,所以选择了nginx做反向代理 一个upstream tomcat_server对应一个  locatio ...

  7. SpringBoot的启动流程分析(1)

    通过分析我们可以找到 org.springframework.boot.SpringApplication 中如下, public static ConfigurableApplicationCont ...

  8. URL tomcat中文乱码

    <Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8" conn ...

  9. matlab中特殊符号如希腊字符

    使用legend 'Best' 图标标识放在图框内不与图冲突的最佳位置'BestOutside' 图标标识放在图框外使用最小空间的最佳位置 legend('sin','cos','location', ...

  10. VIM:Found a swap file by the name

    在linux下用vi或vim打开Test.java文件时 [root@localhost tmp]# vi Test.java出现了如下信息: E325: ATTENTION    Found a s ...