python 内置函数filter


Posted in Python onJune 01, 2017

python 内置函数filter

class filter(object):
 """
 filter(function or None, iterable) --> filter object
 
 Return an iterator yielding those items of iterable for which function(item)
 is true. If function is None, return the items that are true.
 """

filter(func,iterator)

    func:自定义或匿名函数中所得值是布尔值,true将保留函数所取到的值,false则取反。
    iterator:可迭代对象。

例:

     过滤列表['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']
     只要含有text字符串及将其取出 or 取反。

s.rfind'text'+1

     Python3中 rfind() 返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
     数字中0是false,0以上的整数都是true,所以s.rfind'text'后会有+1,没找到字符及-1+1=0.

# Filter

li = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']

# 默认保留函数所取到的值
print(list(filter(lambda s: s.rfind('text') + 1, li)))
# 取反,下三个例子是一样的
print(list(filter(lambda s: not s.rfind('text') + 1, li)))

# Noe 自定义函数

l1 = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']


def distinguish(l):
 nl = []
 for s in l:
  if s.rfind("text") + 1:
   nl.append(s)
 return nl


print(distinguish(l1))

# Two 自定义高阶函数

l2 = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']


def f(s):
 return s.rfind('text') + 1


def distinguish(func, array):
 nl = []
 for s in array:
  if func(s):
   nl.append(s)
 return nl


print(distinguish(f, l2))

# Three 匿名函数

l3 = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']


def distinguish(func, array):
 nl = []
 for s in array:
  if func(s):
   nl.append(s)
 return nl

print(distinguish(lambda s: s.rfind('text') + 1, l3))

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
使用python爬虫实现网络股票信息爬取的demo
Jan 05 Python
python提取具有某种特定字符串的行数据方法
Dec 11 Python
python实现生成字符串大小写字母和数字的各种组合
Jan 01 Python
django框架防止XSS注入的方法分析
Jun 21 Python
关于 Python opencv 使用中的 ValueError: too many values to unpack
Jun 28 Python
python内置函数sorted()用法深入分析
Oct 08 Python
浅谈Django中的QueryDict元素为数组的坑
Mar 31 Python
keras处理欠拟合和过拟合的实例讲解
May 25 Python
零基础学python应该从哪里入手
Aug 11 Python
python 服务器运行代码报错ModuleNotFoundError的解决办法
Sep 16 Python
python代数式括号有效性检验示例代码
Oct 04 Python
matplotlib交互式数据光标实现(mplcursors)
Jan 13 Python
python读取二进制mnist实例详解
May 31 #Python
Python算术运算符实例详解
May 31 #Python
Python简单的制作图片验证码实例
May 31 #Python
详解python的webrtc库实现语音端点检测
May 31 #Python
python实现决策树C4.5算法详解(在ID3基础上改进)
May 31 #Python
基于ID3决策树算法的实现(Python版)
May 31 #Python
Python基础知识_浅谈用户交互
May 31 #Python
You might like
php操作excel文件 基于phpexcel
2010/07/02 PHP
利用PHP实现智能文件类型检测的实现代码
2011/08/02 PHP
PHP隐形一句话后门,和ThinkPHP框架加密码程序(base64_decode)
2011/11/02 PHP
PHP中mb_convert_encoding与iconv函数的深入解析
2013/06/21 PHP
CentOS7.0下安装PHP5.6.30服务的教程详解
2018/09/29 PHP
javascript之AJAX框架使用说明
2010/04/24 Javascript
javascript cookie操作类的实现代码小结附使用方法
2010/06/02 Javascript
基于json的jquery地区联动效果代码
2011/07/06 Javascript
js 动态加载事件的几种方法总结
2013/12/25 Javascript
jquery实现一个简单好用的弹出框
2014/09/26 Javascript
JavaScript中创建字典对象(dictionary)实例
2015/03/31 Javascript
整理Javascript基础语法学习笔记
2015/11/29 Javascript
js操作DOM--添加、删除节点的简单实例
2016/07/08 Javascript
AngularJS表格详解及示例代码
2016/08/17 Javascript
vue-hook-form使用详解
2017/04/07 Javascript
用Electron写个带界面的nodejs爬虫的实现方法
2019/01/29 NodeJs
微信小程序 弹窗输入组件的实现解析
2019/08/12 Javascript
node.js 如何监视文件变化
2020/09/01 Javascript
微信小程序实现弹幕墙(祝福墙)
2020/11/18 Javascript
跟老齐学Python之让人欢喜让人忧的迭代
2014/10/02 Python
Php多进程实现代码
2018/05/07 Python
Python wxpython模块响应鼠标拖动事件操作示例
2018/08/23 Python
详解python的四种内置数据结构
2019/03/19 Python
python将字典列表导出为Excel文件的方法
2019/09/02 Python
wxPython电子表格功能wx.grid实例教程
2019/11/19 Python
python 实现视频 图像帧提取
2019/12/10 Python
Python OrderedDict字典排序方法详解
2020/05/21 Python
建筑工程管理专业自荐信范文
2013/12/28 职场文书
毕业自我评价
2014/02/05 职场文书
秋季校运动会广播稿
2014/02/23 职场文书
法院授权委托书范文
2014/08/02 职场文书
公务员政审材料范文
2014/12/23 职场文书
学校食品安全责任书
2015/01/29 职场文书
水电工岗位职责
2015/02/14 职场文书
小学生2015教师节演讲稿
2015/03/19 职场文书
村级干部党员公开承诺事项
2015/05/04 职场文书