标签:col print 对象 过滤 code span == 输出 strong
filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。
以下是 filter() 方法的语法:
filter(function, iterable)
返回列表。
以下展示了使用 filter 函数的实例:
过滤出列表中的所有奇数: #!/usr/bin/python # -*- coding: UTF-8 -*- def is_odd(n): return n % 2 == 1 newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(list(newlist))
输出结果 :
[1, 3, 5, 7, 9]
标签:col print 对象 过滤 code span == 输出 strong
原文地址:http://www.cnblogs.com/yrxns/p/7760495.html