python filter函数 字符串_Python数组条件过滤filter函数使用示例
生活随笔
收集整理的這篇文章主要介紹了
python filter函数 字符串_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)轉換。
總結
以上是生活随笔為你收集整理的python filter函数 字符串_Python数组条件过滤filter函数使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 语音识别 windows_
- 下一篇: 核磁共振波谱分析_实验室各种仪器原理动图