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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解Python数组条件过滤filter函数使用示例的相关内容吗,在本文为您仔细讲解数组条件过滤的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:数组,条件过滤,filter函数,下面大家一起来学习吧。

使用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)转换。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们