内置函数filter()和匿名函数lambda解析
一.内置函数filter
filter()函数是 Python 内置的一个高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回由符合条件迭代器(python3以下版本返回是列表)。
语法:filter(function or None, iterable) --> filter object
实例:
#获取数字100以内的奇数
def even_num(n):
return n % 2 ==1
res = filter(even_num,(i for i in range(10)))
for i in res:
print(i)
#执行结果:
1
3
5
7
9
# 利用 filter、自定义函数 获取l1中元素大于33的所有元素 l1 = [11, 22, 33, 44, 55]
ll = [11, 22, 33, 44, 55]
def ll_fil(x):
return x > 33
for i in filter(ll_fil,ll):
print(i)
#执行结果:
44
55
二.匿名函数lambda
g = lambda x:x**2
print(g(4))
执行结果:
16
#利用 filter、lambda表达式 获取l1中元素小于33的所有元素 l1 = [11, 22, 33, 44, 55]
ll = [11, 22, 33, 44, 55]
res = filter(lambda x:x<33,ll)
for i in res:
print(i)
#执行结果:
11
22
内置函数filter()和匿名函数lambda解析的更多相关文章
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- Python内置函数二 (递归函数,匿名函数,二分法)
匿名函数 lambda() 语法: lambad 参数 : 返回值 def func(a,b): return a * b print(func(2,5)) a = lambda a ,b : a* ...
- day16_函数作用域_匿名函数_函数式编程_map_reduce_filter_(部分)内置函数
20180729 补充部分代码 20180727 上传代码 #!/usr/bin/env python # -*- coding:utf-8 -*- # ***************** ...
- python----内置函数2与匿名函数
1.迭代器生成器相关 range:创建一个可迭代对象,一般与for混合使用,可设置步长. for i in range(0,10,2): #步长2范围为0-10不包括10 print(i) # 0 2 ...
- angular内置过滤器-filter
这篇文章来讲解一下angular内置的filter过滤器. 没错,这个过滤器的名字,就叫'filter',虽然自定义过滤器也是使用module.filter()...但是不要混淆了,这个filter就 ...
- PYTHON 学习笔记2 流程控制工具以及函数定义、匿名函数
前言 在上一节的学习中.已经介绍了几种基本类型.包括字符串的定义,以及字符串中索引.切片.字符串拼接的使用方法.以及基本的整形数据运算.一些之前都没有了解过的运算符.比如 ** 乘方 //整数除法等. ...
- JavaScript 函数声明,函数表达式,匿名函数,立即执行函数之区别
函数声明:function fnName () {-};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName = function () {-};使 ...
- 深入理解,函数声明、函数表达式、匿名函数、立即执行函数、window.onload的区别.
一.函数声明.函数表达式.匿名函数1.函数声明:function fnName () {…};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 2.函数表达式 var fnNam ...
- Python的程序结构[4] -> 函数/Function[2] -> 匿名函数
匿名函数 / Anonymous Function 匿名函数是一种不需要绑定函数名的函数 (i.e. functions that are not bound to a name).匿名函数通过 la ...
随机推荐
- tomcat启动出错 invalid LOC header
tomcat启动出错 invalid LOC header,run as maven test 没有报错,只有警告: 'build.plugins.plugin.version' for org.ap ...
- python装饰器语法
@就是decorator,早Python的高效开发中会用到,当然和java的annotation有一定的相似,但又不完全相同,看这篇文章:https://blog.csdn.net/zkp_987/a ...
- MySQL查询表与表字段的信息
环境: Mysql数据库 库名:db_name 表名: table_name1 table_name2 查询一个里面所有表的信息: use information_scheam; select * f ...
- F - Currency Exchange
来源poj1860 everal currency exchange points are working in our city. Let us suppose that each point sp ...
- gym 101657 D
理论1A. //没删debug的文件读入.. 傻逼题. 先求出来每条边两侧的三角形,然后枚举边,根据叉积判断三角形位置,建图,拓扑排序. #include <bits/stdc++.h> ...
- java导入、导出Excel文件
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- mysql sql注入getshell新姿势
sql.php?sql=1'报错信息为:1064:You have an error in your SQL syntax; check the manual that corresponds to ...
- 记一次Springboot启动异常
启动Springboot项目报以下异常: org.springframework.context.ApplicationContextException: Unable to start web se ...
- CF3A Shortest path of the king
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- Yarn Node Labels
Yarn Node Labels + Capacity-Scheduler 在yarn-site.xml中开启capacity-schedule yarn-site.xml <property& ...