python filter

Python filter() function is used to filter the elements of an iterable based on a function. This function returns filter object that is an iterator.

Python filter()函数用于基于函数过滤可迭代的元素。 此函数返回作为迭代器的 filter对象。

Python filter() (Python filter())

Python filter() function syntax is:

Python filter()函数语法为:

filter(function, iterable)

function will be called on iterable elements and if it returns True then they will be part of the returned iterator.

function将在iterable元素上调用,如果返回True则它们将成为返回的迭代器的一部分。

We can also pass the function as None, in that case, standard truth testing rules will be followed to determine if the iterable elements are True or False. You can get more details about them in Python bool example.

我们还可以将函数传递为None ,在这种情况下,将遵循标准的真相测试规则来确定可迭代元素是True还是False。 您可以在Python bool示例中获取有关它们的更多详细信息。

Python filter()示例 (Python filter() example)

Let’s define a function to check if the input number is even or not. It will return True if the number is even, else False.

让我们定义一个函数来检查输入数字是否为偶数。 如果数字为偶数,它将返回True,否则返回False。

We will also define a utility function to print elements of the iterator.

我们还将定义一个实用程序函数来打印迭代器的元素。

def is_even(x):if x % 2 == 0:return Trueelse:return Falsedef print_filter_items(my_filter):for item in my_filter:print(item, end=' ')print()

Let’s use filter() function to get an iterator of even numbers only from the input iterable of integers. We will use tuple and list for our example, both of them are iterable.

让我们使用filter()函数仅从输入的可迭代整数中获取偶数的迭代器。 我们将在示例中使用元组和列表 ,它们都是可迭代的。

l1 = [1, 2, 3, 4, 5]
fl = filter(is_even, l1)
print(type(fl))
print_filter_items(fl)t = (1, 2, 3, 4, 5)
fl = filter(is_even, t)
print_filter_items(fl)

Output:

输出:

<class 'filter'>
2 4
2 4

带有None函数的Python filter()示例 (Python filter() example with None function)

t = (True, False, 1, 0, 0.0, 0.5, '', 'A', None)
fl = filter(None, t)
print_filter_items(fl)

Output:

输出:

True 1 0.5 A

Notice that zero, empty string, False and None are filtered out because their boolean value is False.

请注意,零,空字符串,False和None被过滤掉,因为它们的布尔值是False。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22816/python-filter

python filter

python filter_Python filter()相关推荐

  1. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  2. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  3. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  4. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  5. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  6. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

  7. Python字符串splitlines()

    Python String splitlines() Python字符串splitlines() Python String splitlines() function returns the lis ...

  8. Python字符串join()方法

    Python string join() method creates a string from an iterable. It joins all the iterable elements wi ...

  9. Python字符串index()

    Python String index() function returns the lowest index where the specified substring is found. If t ...

最新文章

  1. 无线信标功能初步测试
  2. 【PP】SAP库存决定
  3. Jmeter报告优化之New XSL stylesheet
  4. 十余位权威专家深度解读,达摩院2019十大科技趋势点燃科技热情
  5. ensp查看历史配置命令_网络工程师 | 手把手教你用华为ensp模拟器玩vxlan实验(静态方式)...
  6. Windows系统安全从定制IP策略开始
  7. php 进行http请求,php模拟http请求的两种方式
  8. 系统服务启动交互式程序(C++)
  9. ConnectivityManager
  10. Filter 敏感词汇过滤案例
  11. 最强Redis实战学习笔记,没有之一!
  12. VC编程中20种各种编程技巧和方法
  13. 在python如何调用三角函数_Python中计算三角函数之cos()方法的使用简介
  14. 键盘符号中英文对照表
  15. 洛谷P4325 [COCI2006-2007#1] Modulo
  16. 吴乙己的数仓指南_2维度建模核心思想
  17. OSChina 周六乱弹 —— 周末万岁!
  18. 为什么要学习 Linux?
  19. likely()和unlikely()
  20. java面试题,输入一串数字,输出大写金额,如123 输出壹佰贰拾叁整。

热门文章

  1. MySQL_采购入库价格与在线售价监控_20161213
  2. Obective-C之宏定义
  3. [转载] python 需求清单_Python清单操作摘要
  4. [转载] python字符串数组字典_Python:字符串、列表、元组、字典
  5. [转载] real和imag在python_Python numpy.imag() 使用实例
  6. [转载] 基本概念:java中的访问修饰符
  7. JavaScript正则表达式补充
  8. 关于androidAsyncHttp支持https
  9. UFLDL 教程学习笔记(二)反向传导算法
  10. Mutex对象是操作系统级?