Python的lambda表达式、filter、map、reduce等函数的用法
lambda是表达式,用于创建匿名函数,可以和filter、map、reduce配合使用。
本文环境Python3.7。
一、lambda表达式
lambda 只包含一个语句,用于创建匿名函数。
语法:
lambda [arg1 [,arg2,.....argn]]:expression
arg1 -- 参数,可以有多个
expression -- 表达式
使用例子:
f1 = lambda x : x > 10
print(f1(1)) #输出:False
print(f1(11)) #输出:True f2 = lambda x, y: x + y
print(f2(1,2)) #输出:3
二、filter() 函数
filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回迭代器对象(Python2是列表),可以使用list()转换为列表。
语法:
filter(function, iterable)
function -- 函数
iterable -- 序列,序列的每个元素作为参数传递给函数进行判断,然后返回 True 或 False,最后将返回 True 的元素放到迭代器对象中。
使用例子:
def isEven(x):
return x % 2 == 0 list1 = filter(isEven,[1,2,3,4,5,6])
print(list(list1)) #输出:[2, 4, 6] #可以用lambda
list2 = filter(lambda x:x%2==0, [1,2,3,4,5,6])
print(list(list2)) #输出:[2, 4, 6] #也可以用列表推导式
list3 = list(x for x in [1,2,3,4,5,6] if x%2==0)
print(list3) #输出:[2, 4, 6]
三、map()函数
map()接收一个函数 f 和一个或多个序列 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 迭代器(Python2是列表) 并返回。
语法:
map(function, iterable, ...)
function -- 函数,如果iterable有多个序列,则function的参数也对应有多个
iterable -- 一个或多个序列
使用例子:
def multi(x):
return x * 2 def multi2(x, y):
return x * y #参数只有1个序列时
list1 = map(multi,[1,2,3,4,5])
print(list(list1)) #输出:[2, 4, 6, 8, 10] #用lambda改写上面语句
list1_1 = map(lambda x : x*2, [1,2,3,4,5])
print(list(list1_1)) #输出:[2, 4, 6, 8, 10] #参数有2个序列时,
list2 = map(multi2,[1,2,3,4,5],[6,7,8,9,10])
print(list(list2)) #对2个列表数据的相同位置元素相乘,输出:[6, 14, 24, 36, 50] #用lambda改写上面语句
list2_1 = map(lambda x,y : x*y, [1,2,3,4,5],[6,7,8,9,10])
print(list(list2_1)) #输出:[6, 14, 24, 36, 50] #当2个序列长度不一致时,结果以2个序列中的最短长度为准
list2_2 = map(lambda x,y : x*y, [1,2,3],[6,7,8,9,10])
print(list(list2_2)) #输出:[6, 14, 24]
list2_3 = map(lambda x,y : x*y, [1,2,3,4,5],[6,7,8])
print(list(list2_3)) #输出:[6, 14, 24]
四、reduce()函数
reduce()函数对一个数据集合的所有数据进行操作:用传给 reduce 中的函数 function(必须有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。
在Python2中reduce()是内置函数,Pytho3移到functools 模块。
语法:
reduce(function, iterable[, initializer])
function -- 函数,有两个参数
iterable -- 可迭代对象
initializer -- 可选,初始参数
使用例子:
from functools import reduce def add(x, y):
return x + y
def mulit(x, y):
return x * y print(reduce(add, [1, 2, 3, 4, 5])) #输出:15
print(reduce(add, [1, 2, 3, 4, 5], 10)) #输出:25 print(reduce(mulit, [1, 2, 3, 4, 5])) #输出:120
print(reduce(mulit, [1, 2, 3, 4, 5], 10)) #输出:1200 print(reduce(lambda x,y:x+y,[1, 2, 3, 4, 5]))#输出:15
print(reduce(lambda x,y:x+y,[1, 2, 3, 4, 5], 10))#输出:25
Python的lambda表达式、filter、map、reduce等函数的用法的更多相关文章
- Python中特殊函数和表达式 filter,map,reduce,lambda
1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...
- Python中特殊函数和表达式lambda,filter,map,reduce
1.lambda:使用lambda表达式可以定义一个匿名函数 lambda表达式是一种简洁格式的函数.该表达式不是正常的函数结构,而是属于表达式的类型 (1)基本格式: lambda 参数,参数... ...
- Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数
python一切皆对象,linux一切皆文件,python操作文件是很常见的O/I操作,其内置来open()函数可以完成文件的基本操作: 一:使用内置open()函数操作文件,基本语法如下: with ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- 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,apply,zip】
Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- python filter map reduce
filter(function, iterable): Construct a list from those elements of iterable for which function retu ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
随机推荐
- Linux 磁盘分区、挂载
一.分区介绍 mbr分区: 1.最多支持四个主分区 2.系统只能安装在主分区上 3.扩展分区要占一个主分区 4.mbr最大只支持2TB,但拥有最好的兼容性 gpt分区: 1.支持无限多个主分区(但操作 ...
- 在vue中添加ico图标
准备:添加 ico图标在与index.html同级的目录 第一种方法: 在index.html中引入: <link rel="shortcuticon" type=" ...
- [转]VBA Check if an outlook folder exists; if not create it
本文转自:http://www.outlookcode.com/d/code/quarexe.htm To quarantine application file attachments This O ...
- angularjs用回车键动态添加数据,同时渲染到页面
<script src="../../angular-1.5.5/angular.min.js"></script> <script> var ...
- Spring Boot 2 + Thymeleaf:表单字段绑定、表单提交处理
Spring Boot中Thymeleaf对表单处理的一些用法:(1)使用th:field属性:进行表单字段绑定(2)使用ids对象:一般用于lable配合radio或checkbox使用(3)表单提 ...
- ABP入门教程6 - 领域层创建实体
点这里进入ABP入门教程目录 创建实体 在领域层(即JD.CRS.Core)下新建文件夹Entitys //用以存放实体对象添加一个实体类Course.cs //课程信息 using Abp.Doma ...
- MySQL基础之数据管理【4】
外键约束的使用(只有InnoDB存储引擎支持外键) create table news_cate( id tinyint unsigned auto_increment key comment '编号 ...
- springboot入门以及配置文件
springboot入门以及配置文件 SpringBoot是什么? Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架的应用 ...
- luoguP3649 [APIO2014]回文串
题意 关于回文自动机的讲解见这里 由于回文串个数是\(O(n)\)的,直接回文自动机上统计并比较即可. code: #include<bits/stdc++.h> using namesp ...
- [译]ABP v1.0终于发布了!
ABP v1.0终于发布了! 今天是个大日子!经过约3年的不断开发,第一个稳定的ABP版本,1.0,已经发布了.感谢为该项目做出贡献或试用过的每个人. 立即开始使用新的ABP框架:abp.io/get ...