Python数组条件过滤filter函数使用示例

Freya ·
更新时间:2024-09-21
· 866 次阅读

使用filter函数,实现一个条件判断函数即可。

比如想过滤掉字符串数组中某个敏感词,示范代码如下:

#filter out some unwanted tags def passed(item): try: return item != "techbrood" #can be more a complicated condition here except ValueError: return False org_words = [["this","is"],["demo","from"],["techbrood"]] words = [filter(passed, item) for item in org_words]

注意Python2.x和Python3.x对于filter/map的处理并不兼容,在Python2.x里面直接返回一个list.

在Python3.x里返回一个iterable对象,比如<filter object at 0x000000000251C978>,后面那串数字是对象引用地址。

可以使用list(words)转换。

您可能感兴趣的文章:python3去掉string中的标点符号方法Python简单过滤字母和数字的方法小结Python实现简单过滤文本段的方法python过滤中英文标点符号的实例代码



filter filter函数 示例 python数组 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号