源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-01-10 15:39 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python数组条件过滤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)转换。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部