day16 Python filter函数
前戏
movie_people = ["alex","charon","pluto","liu","sb","sb_250"] ret = []
for i in movie_people:
if not i.startswith("sb"):
ret.append(i) print(ret) 结果:
['alex', 'charon', 'pluto', 'liu']
前五分钟
movie_people = ["alex","charon","pluto","liu","sb","sb_250"] def filter_test(arrat):
ret = []
for i in arrat:
if not i.startswith("sb"):
ret.append(i)
return ret print(filter_test(movie_people)) 结果:
['alex', 'charon', 'pluto', 'liu']
十分钟
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
def sb_show(n):
return n.endswith('sb') def filter_test(func,array):
ret=[]
for p in array:
if not func(p):
ret.append(p)
return ret res=filter_test(sb_show,movie_people)
print(res) 结果:
linhaifeng
十五分钟
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# def sb_show(n):
# return n.endswith('sb')
#--->lambda n:n.endswith('sb')
# :前面是参数,后面是返回值
def filter_test(func,array):
ret=[]
for p in array:
if not func(p):
ret.append(p)
return ret res=filter_test(lambda n:n.endswith('sb'),movie_people)
print(res) #filter函数
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
print(filter(lambda n:not n.endswith('sb'),movie_people)) res=filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res)) print(list(filter(lambda n:not n.endswith('sb'),movie_people))) 结果:
['linhaifeng']
<filter object at 0x7f9aa857b908>
['linhaifeng']
['linhaifeng']
day16 Python filter函数的更多相关文章
- Python filter() 函数
Python filter() 函数 描述 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为 ...
- python filter函数(40)
一.filter函数简介 filter函数主要用来筛选数据,过滤掉不符合条件的元素,并返回一个迭代器对象,如果要转换为列表list或者元祖tuple,可以使用内置函数list() 或者内置函数tupl ...
- py-day4 python filter函数
filter函数:遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下 # 例子:条件筛选 name =['m_xiaoli','zhangfei','m_xiaoma','m_wang ...
- python filter函数应用,过滤字符串
>>> candidate = 'dade142.;!0142f[.,]ad' >>> filter(str.isdigit, candidate) #保留数字 ' ...
- python filter函数
number_list = range(-, ) less_than_zero = list(filter(lambda x: x < , number_list)) print(less_th ...
- day16 Python map函数
num_l=[1,2,10,5,3,7] #lambda x:x+1 # def add_one(x): # return x+1 #lambda x:x+1 # def reduce_one(x): ...
- filter(函数,可以迭代的对象)
#!/usr/bin/env python #filter(函数,可以迭代的对象) def f1(x): if x > 22: return True else: return False re ...
- python基础——filter函数
python基础——filter函数 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函 ...
- python的filter()函数
filter()函数是 Python 内置的另一个有用的高阶函数. filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,fil ...
随机推荐
- win10 关闭自动更新
方法一 : 利用组策略关闭win10自动更新的步骤如下:1.按win+R打开“运行”,输入“gpedit.msc”,按下回车. 2.找到“计算机配置”→““管理模板”→“Windows 组件”→“Wi ...
- Java并发编程:线程池的使用(转载)
转载自:https://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...
- Modular Inverse(zoj3609+欧几里德)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- java - Jsoup原理
https://blog.csdn.net/xh16319/article/details/28129845 http://www.voidcn.com/article/p-hphczsin-ru.h ...
- Python带你轻松进行网页爬虫
前不久DotNet开源大本营通过为.NET程序员演示如何在.NET下使用C#+HtmlAgilityPack+XPath进行网页数据的抓取,从而为我们展示了HtmlAgilitypack利器的优点和使 ...
- Nginx负载均衡配置调优
worker_processes ; error_log logs/error.log crit; #日志位置和日志级别 pid logs/nginx.pid; worker_rlimit_nofil ...
- CSS外观属性
CSS外观属性 color 文本颜色 line-height 行间距 text-aline水平对齐方式 text-indent 首行缩进 letter-spacing字间距 word-spacing ...
- jpa 联表查询 返回自定义对象 hql语法 原生sql 语法 1.11.9版本
-----业务场景中经常涉及到联查,jpa的hql语法提供了内连接的查询方式(不支持复杂hql,比如left join ,right join). 上代码了 1.我们要联查房屋和房屋用户中间表,通过 ...
- memcached分析
memcache介绍 memcache是一个高性能的分布式的内存对象缓存系统,用于动态Web应用以减轻数据库负担.它通过在内存中缓存数据和对象,来减少读取数据库的次数.从而提高动态.数据库驱动网站速度 ...
- [20171120]关于find 软连接问题.txt
[20171120]关于find 软连接问题.txt --//上个星期为了测试oracle参数filesystemio_options,将数据库做了一次移动.但是我使用find对软链接目录查询时--/ ...