Python基础-高阶函数-filter()
2017-12-11 14:43
441 查看
filter()-过滤序列
参数:函数序列
filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
取奇数序列
示例#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Python filter 用法 # 是否为奇数 def isOdd(x): return (x % 2) != 0 def filterTest(): # 函数,序列 result = filter(isOdd, [1, 2, 3,4,5]) print(result) filterTest()
运行结果
D:\PythonProject>python run.py [1, 3, 5]
删除空字符串
示例代码#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Python filter 用法 # 删除空字符串 def whithoutEmptyString(s): return s and s.strip() def filterTest(): # 函数,序列 result = filter(whithoutEmptyString, ["A", "", "B"]) print(result) filterTest()
运行结果
D:\PythonProject>python run.py ['A', 'B']
相关文章推荐
- 零基础入门学习Python(12):lambda表达式、filter()和map()
- python 高阶函数:sort,filter,reduce,map
- Python学习2--高阶函数map、reduce、filter、sorted
- python基础知识(三)python高阶函数
- Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数
- Python基础-高阶函数-Higher-order function
- python高阶函数filter
- python基础之map/reduce/filter/sorted
- Python进阶(十四)- 基础课程结课总结:高阶函数
- Python高阶函数——map/reduce/filter/sorted
- Python 函数式编程(高阶函数、把函数作为参数、map()函数、reduce()函数、filter()函数、自定义排序函数、函数返回函数、闭包、匿名函数、装饰器decorator)
- Python高阶函数map&reduce&filter总结
- 不得不知的python高阶函数(Map、Reduce、Filter)
- 【Python】学习笔记——-5.1.2、高阶函数之filter
- Python基础8--内置高阶函数
- python学习(9)高阶函数map、reduce、filter、sorted
- Python高阶函数(Map、Reduce、Filter)和lambda函数一起使用 ,三剑客
- Python3 基础:高阶函数和map/reduce
- python函数基础:嵌套函数、作用域、匿名函数、高阶函数、递归函数
- python基础---高阶函数