当前位置: 编程技术>python
Python数组条件过滤filter函数使用示例
来源: 互联网 发布时间:2014-10-09
本文导语: 使用filter函数,实现一个条件判断函数即可。 比如想过滤掉字符串数组中某个敏感词,示范代码如下: #filter out some unwanted tags def passed(item): try: return item != "techbrood" #can be more a complicated condition here except ValueError: return...
使用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对象,比如,后面那串数字是对象引用地址。
可以使用list(words)转换。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。