原创转载请注明出处:http://agilestyle.iteye.com/blog/2330331
Python内置的filter()函数用于过滤序列
过滤偶数
def is_odd(n): return n % 2 == 1 # [1, 3, 5, 7, 9] print(list(filter(is_odd, list(range(1, 10)))))
过滤空字符串
def not_empty(s): return s and s.strip() # ['a', 'b'] print(list(filter(not_empty, ['a', 'b', '', None])))
Console Output
用filter求30以内的素数
def odd_iter(): n = 1 while True: n = n + 2 yield n def not_divisible(n): return lambda x: x % n > 0 def primes(): yield 2 it = odd_iter() while True: n = next(it) yield n it = filter(not_divisible(n), it) for i in primes(): if i < 30: print(i) else: break
Console Output
Python内置的sorted()函数就可以对list进行排序
# [-55, -44, 22, 33, 66, 77, 88] print(sorted([88, 77, 66, -55, -44, 33, 22])) # [22, 33, -44, -55, 66, 77, 88] print(sorted([88, 77, 66, -55, -44, 33, 22], key=abs)) # ['HBase', 'HDFS', 'Hadoop', 'Hive', 'MapReduce'] print(sorted(['Hadoop', 'HDFS', 'MapReduce', 'Hive', 'HBase'])) # ['Hadoop', 'HBase', 'HDFS', 'Hive', 'MapReduce'] print(sorted(['Hadoop', 'HDFS', 'MapReduce', 'Hive', 'HBase'], key=str.lower)) # ['MapReduce', 'Hive', 'HDFS', 'HBase', 'Hadoop'] print(sorted(['Hadoop', 'HDFS', 'MapReduce', 'Hive', 'HBase'], key=str.lower, reverse=True))
Console Output
相关推荐
文章目录sorted函数一、sort方法二、sorted内置函数三、情景引入filter类一、简单使用二、练习map类语法:一、简单使用二、练习reduce函数语法:一、简单使用二、设置初始值 Python中使用函数作为参数的内置函数和类...
高阶函数 filter&sorted;.mp4 │ 千锋Python教程:39.作用域&修改变量作用域.mp4 │ ├─千锋Python教程:第07章 闭包&装饰器(5集) │ │ .DS_Store │ │ │ ├─code │ │ 10、多个装饰器.py │ │ 11、装饰...
4-5函数式编程:map_reduce_filter_sorted_偏函数 5-1列表生成式 5-2迭代器 5-3生成器 5-4装饰器 5-5Python编程规范 6-1模块名称空间和导入 6-2模块的执行 6-3os和sys模块介绍和使用 6-4第三方模块的安装 7-1类与...
主要介绍了Python中map,reduce,filter和sorted函数的使用方法,是Python入门学习中的基础知识,需要的朋友可以参考下
Python’s easy-to-learn and extensible abilities offer a number of functional programming features ...Use functions like max(), min(), map(), filter(), and sorted() Write advanced higher-order functions
Python学习过程中,遇到的python...高级特性,包括切片、列表生成式,生成器、迭代器,函数式编程,包括高级函数、map、reduce、filter、sorted;返回函数、匿名函数、偏函数、装饰器的深入理解。面向对象高级编程等。
Python语法总结,语法学习讲义和笔记。 部分目录: 1. Python 3 1.1. 数据类型: 3 1.1.1. 整数,浮点数,字符串,...1.4.5. filter 8 1.4.6. sorted 9 1.4.7. 匿名函数 9 1.4.8. 装饰器 9 1.4.9. 偏函数 10 1.5. 类 10
filter 130 sorted 134 返回函数 137 匿名函数 141 装饰器 143 偏函数 148 模块 151 使用模块 153 安装第三方模块 157 面向对象编程 159 类和实例 161 访问限制 165 继承和多态 169 获取对象信息 175 ...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...
filter sorted 返回函数 匿名函数 装饰器 偏函数 模块 使用模块 安装第三方模块 单元测试 文档测试 IO编程 文件读写 StringIO和BytesIO 操作文件和目录 序列化 进程和线程 多进程 多线程 ThreadLocal 进程 vs. 线程 ...
1.10 Using List Comprehensions Instead of map and filter 1.11 Unzipping Simple List-Like Objects 1.12 Flattening a Nested Sequence 1.13 Looping in Parallel over Index and Sequence Items 1.14 ...
Looking for Items in a Sorted Sequence Recipe 5.10. Selecting the nth Smallest Element of a Sequence Recipe 5.11. Showing off quicksort in Three Lines Recipe 5.12. Performing Frequent ...
filter: sorted: 函数式编程 首先来看一段函数式编程的定义,不想看的先跳过,结合具体实例后再来理解吧! 函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有 变量,因此,任意一个...
filter(func, iterable) sorted(iterable, key=None, reverse=False) sorted('AGFBDC') sorted('ACDacbdE') # ['A', 'C', 'D', 'E', 'a', 'b', 'c', 'd'] # 按字符顺序排序,不区分大小写 def f(ch): code = ...
目录: 01 教程.png 01.01 2.x与3.x版本区别.png 02 基础语法.png 02.01 命令行参数.png 03 基本数据类型.png 03.01 数据类型转换 int() 函数.png 03.02 数据类型转换 float() 函数.png ...
介绍Python用于支持将函数赋值给变量的一个操作符默认是返回的,所以不用再加return关键字,不然会报错需要两个参数,第一个是一个处理函数,第二个是一个序列