Python中特殊函数和表达式 filter,map,reduce,lambda
1. filter
官方解释:filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
传入参数:function或None,序列
功能:将按照function中定义的方式进行过滤,返回True的留下,返回False的剔除。
#coding=utf-8
def findPositiveNum(num):
if num > 0:
return True
else:
return False list={2,1,3,-2,0,12,5,-9}
print filter(findPositiveNum,list)
返回结果:
[1, 2, 3, 5, 12]
2. map
官方解释:
map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
传入参数:function或None,序列
功能:将序列的中的各个参数传入function中作为参数执行,返回的结果就是序列的值传入function中执行的结果,是一个序列。如果function为None,那么返回值为序列本身。
def multiValue(num):
return num**2 list1={1,3,5,7,9,11}
print map(multiValue,list1)
返回结果:
[1, 9, 25, 49, 81, 121]
如果function传入为None则返回值为
[1, 3, 5, 7, 9, 11]
3. 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.
将两个参数的函数累积到序列项上,从左到右,以将序列减少到单个的子序列示例中,减少(λx,y:x+y,[1,2,3,4,5])计算(1+2)+3)+4)。如果初始存在,则将其放在计算序列项之前,当序列为空时作为默认值。
def addNum(num1,num2):
return num1+num2 list2={1,2,3,4,5,6}
print reduce(addNum, list2)
代码解释:先计算1+2的值,然后将计算的值作为第一个参数传入addNum中,第二个参数为3,依次类推。 最终结果为:
21
如果传入初始值得话则:
def addNum(num1,num2):
return num1+num2 list2={1,2,3,4,5,6}
#print reduce(addNum, list2)
print reduce(addNum, list2,100)
结果为:
121
4. lambda 可以用来定义匿名函数
addnum= lambda x,y:x+y
print addnum(1,2)
multiNum=lambda x:x**2
print multiNum(5)
结果为
3
25
Python中特殊函数和表达式 filter,map,reduce,lambda的更多相关文章
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- filter,map,reduce,lambda(python3)
1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
- Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted
1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...
- Python中特殊函数和表达式lambda,filter,map,reduce
1.lambda:使用lambda表达式可以定义一个匿名函数 lambda表达式是一种简洁格式的函数.该表达式不是正常的函数结构,而是属于表达式的类型 (1)基本格式: lambda 参数,参数... ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- python: filter, map, reduce, lambda
filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
随机推荐
- iOS自动化探索(六)自动化测试框架pytest - fixtures
Fixture介绍 fixture是pytest特有的功能,它用pytest.fixture标识,定义在函数前面.在编写测试函数的时候,可以将此函数名称做为传入参数,pytest将会以依赖注入方式,将 ...
- Hosts文件路径及修改方法
(转自:http://www.techolics.com/softdev/20111029_100.html) 什么是Hosts文件? 根据百度百科的定义,Hosts文件是一个系统文件,这是一个本地的 ...
- Linux各文件及目录说明2018-03-01更新
本人wechat:YWNlODAyMzU5MTEzMTQ=. *** /etc /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/clo ...
- 2017.11.17 Demo-stm8+temperature timeing control
1Find the lab and add in project. Downtown it from ST official website..compile it to ensure it pa ...
- H264子宏块的划分有哪些?
每个分割或子宏块都有一个独立的运动补偿.每个 MV 必须被编码.传输,分割的选择也需编 码到压缩比特流中.对大的分割尺寸而言,MV 选择和分割类型只需少量的比特,但运动补偿残差 在多细节区域能量将非常 ...
- 热烈祝贺博主LZUGIS博客访问量突破
截止发文时间,博主"LZUGIS"CSDN博客文章总访问量突破50W,值此特殊的时刻,特发此文,以表纪念与督促. 博客详情 博客专栏 公众号 常言道:不积跬步,无以至千里:不积小流 ...
- 【MFC】MFC DLEdit 设计属于自己的编辑框_鼠标悬停
MFC DLEdit 设计属于自己的编辑框 2012-02-04 13:00 by 捣乱小子, 3543 阅读, 5 评论, 收藏, 编辑 起因 无意间看到了大牛们写的自定义编辑框控件,于是找了个时间 ...
- linux中的vim编辑器的使用
vim的三种模式: 常规模式(命令模式)也是默认模式,从其它模式进行命令模式按esc i 在光标前插入文本 o 命令是指在当前行下方插入新行 dd 是删除光标所在的整个一行 yy 是在光标所在整个放入 ...
- 解决Net内存泄露原因
Net内存泄露原因及解决办法 https://blog.csdn.net/changtianshuiyue/article/details/52443821 什么是.Net内存泄露 (1).NET 应 ...
- test20181219 奇怪的函数
题意 奇怪的函数 [问题描述] 使得x^x达到或超过n位数字的最小正整数x是多少? [文件输入] 输入一个正整数n(n<=2*10^9). [文件输出] 输出使得x^x达到n位数字的最小正整数x ...