手机
当前位置:查字典教程网 >脚本专栏 >python >Python数组条件过滤filter函数使用示例
Python数组条件过滤filter函数使用示例
摘要:使用filter函数,实现一个条件判断函数即可。比如想过滤掉字符串数组中某个敏感词,示范代码如下:#filteroutsomeunwante...

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

【Python数组条件过滤filter函数使用示例】相关文章:

Python urlopen()函数 示例分享

python del()函数用法

python实现图片批量剪切示例

python合并文本文件示例

python学习笔记:字典的使用示例详解

python求斐波那契数列示例分享

Python中的CURL PycURL使用例子

Python的函数嵌套的使用方法

python使用cookielib库示例分享

python 文件和路径操作函数小结

精品推荐
分类导航