`

Python filter sorted

 
阅读更多

原创转载请注明出处: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


 

 

 

  • 大小: 1.6 KB
  • 大小: 8.2 KB
  • 大小: 12.3 KB
分享到:
评论

相关推荐

    Python中sorted函数、filter类、map类、reduce函数

    文章目录sorted函数一、sort方法二、sorted内置函数三、情景引入filter类一、简单使用二、练习map类语法:一、简单使用二、练习reduce函数语法:一、简单使用二、设置初始值 Python中使用函数作为参数的内置函数和类...

    2019千峰Python超详细入门教程(百度云盘分享).docx

    高阶函数 filter&sorted;.mp4 │ 千锋Python教程:39.作用域&修改变量作用域.mp4 │ ├─千锋Python教程:第07章 闭包&装饰器(5集) │ │ .DS_Store │ │ │ ├─code │ │ 10、多个装饰器.py │ │ 11、装饰...

    Python编程零基础入门

    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中map,reduce,filter和sorted函数的使用方法,是Python入门学习中的基础知识,需要的朋友可以参考下

    Functional Python Programming(PACKT,2015)

    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学习过程中,遇到的python...高级特性,包括切片、列表生成式,生成器、迭代器,函数式编程,包括高级函数、map、reduce、filter、sorted;返回函数、匿名函数、偏函数、装饰器的深入理解。面向对象高级编程等。

    Python语法总结

    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

    Python 3教程(廖雪峰).rar

    filter 130 sorted 134 返回函数 137 匿名函数 141 装饰器 143 偏函数 148 模块 151 使用模块 153 安装第三方模块 157 面向对象编程 159 类和实例 161 访问限制 165 继承和多态 169 获取对象信息 175 ...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    廖雪峰python3 完整带索引,图片 最新教程 pdf版

    filter sorted 返回函数 匿名函数 装饰器 偏函数 模块 使用模块 安装第三方模块 单元测试 文档测试 IO编程 文件读写 StringIO和BytesIO 操作文件和目录 序列化 进程和线程 多进程 多线程 ThreadLocal 进程 vs. 线程 ...

    Python Cookbook英文版

    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 ...

    Python Cookbook, 2nd Edition

    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 ...

    Python之函数式编程和高阶函数(map、reduce、filter、sorted)

    filter: sorted: 函数式编程 首先来看一段函数式编程的定义,不想看的先跳过,结合具体实例后再来理解吧! 函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有 变量,因此,任意一个...

    python基础3day01.txt

    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 = ...

    Python3 菜鸟查询手册

    目录: 01 教程.png 01.01 2.x与3.x版本区别.png 02 基础语法.png 02.01 命令行参数.png 03 基本数据类型.png 03.01 数据类型转换 int() 函数.png 03.02 数据类型转换 float() 函数.png ...

    coldnight#coldnight.github.com#lambda-结合map-filter-reduce-sorted

    介绍Python用于支持将函数赋值给变量的一个操作符默认是返回的,所以不用再加return关键字,不然会报错需要两个参数,第一个是一个处理函数,第二个是一个序列

Global site tag (gtag.js) - Google Analytics