Python数组条件过滤filter函数使用示例
使用filter函数,实现一个条件判断函数即可。
比如想过滤掉字符串数组中某个敏感词,示范代码如下:
#filteroutsomeunwantedtags defpassed(item): try: returnitem!="techbrood"#canbemoreacomplicatedconditionhere exceptValueError: returnFalse org_words=[["this","is"],["demo","from"],["techbrood"]] words=[filter(passed,item)foriteminorg_words]
注意Python2.x和Python3.x对于filter/map的处理并不兼容,在Python2.x里面直接返回一个list.
在Python3.x里返回一个iterable对象,比如<filterobjectat0x000000000251C978>,后面那串数字是对象引用地址。
可以使用list(words)转换。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短