前戏

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函数的更多相关文章

  1. Python filter() 函数

    Python filter() 函数 描述 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为 ...

  2. python filter函数(40)

    一.filter函数简介 filter函数主要用来筛选数据,过滤掉不符合条件的元素,并返回一个迭代器对象,如果要转换为列表list或者元祖tuple,可以使用内置函数list() 或者内置函数tupl ...

  3. py-day4 python filter函数

    filter函数:遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下 # 例子:条件筛选 name =['m_xiaoli','zhangfei','m_xiaoma','m_wang ...

  4. python filter函数应用,过滤字符串

    >>> candidate = 'dade142.;!0142f[.,]ad' >>> filter(str.isdigit, candidate) #保留数字 ' ...

  5. python filter函数

    number_list = range(-, ) less_than_zero = list(filter(lambda x: x < , number_list)) print(less_th ...

  6. 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): ...

  7. filter(函数,可以迭代的对象)

    #!/usr/bin/env python #filter(函数,可以迭代的对象) def f1(x): if x > 22: return True else: return False re ...

  8. python基础——filter函数

    python基础——filter函数 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函 ...

  9. python的filter()函数

    filter()函数是 Python 内置的另一个有用的高阶函数. filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,fil ...

随机推荐

  1. vuex学习及使用

    什么是vuex? 在SPA单页面组件的开发中vuex称为状态管理:简单的理解就是你在state中定义了一个数据之后,你可以在所在项目中的任何一个组件里进行获取.进行修改,并且你的修改可以得到全局的响应 ...

  2. angularjs小练习(分别通过ng-repeat和ng-option动态生成select下拉框)

    本次做一个简单的关于动态生成select的练习 在实现上有两种方式: 其一.通过ng-repeat来实现 其二.通过ng-option来实现 在页面效果上,两种实现的效果都一样 但是在数据选择的数据从 ...

  3. autocomplate 学习

    方法一 :[使用插件] // 联想功能 stat function cselstreet() { var name = ""; //$("#txtname"). ...

  4. [android] 手机卫士手机实现短信指令获取位置

    获取位置  新建一个service的包 新建一个GPSService类继承系统的Service类 清单文件中注册一下 重写onCreate()方法,服务创建的时候回调 重写onDestroy()方法, ...

  5. 【github&&git】1、github中的watch、star、fork的作用

    [转自:http://www.jianshu.com/p/6c366b53ea41] 在每个 github 项目的右上角,都有三个按钮,分别是 watch.star.fork,但是有些刚开始使用 gi ...

  6. C#特性之数据类型

    这篇文章主要通过演示类在不同发展中的不通过定义方法,来向读者表述它们之间的区别和联系. 在C#1时代,我们喜欢这样定义类: public class Product { private string ...

  7. 《移山之道:VSTS软件开发指南》读书笔记

    这两天看了<移山之道:VSTS软件开发指南>,对团队软件开发又有了新的认识.也许对于我们这些软件开发的新手来说,最重要的是具体技术与应用框架,但读了这本书后我感觉到,实际团队项目中工具的使 ...

  8. blfs(systemd版本)学习笔记-编译安装openssh软件包

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! openssh项目地址:http://www.linuxfromscratch.org/blfs/view/stable/pos ...

  9. Django Rest framework 之 权限

    django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) django res ...

  10. SQL Anywhere5.5: Metadata

    http://dcx.sybase.com/1101/en/dbprogramming_en11/ianywhere-data-sqlanywhere-saconnection-getschem633 ...