Home  >  Article  >  Backend Development  >  Python filters out a sensitive word in a string array

Python filters out a sensitive word in a string array

伊谢尔伦
伊谢尔伦Original
2017-04-29 10:10:513894browse

Use the filter function to implement a conditional judgment function.

For example, if you want to filter out a certain sensitive word in a string array, the sample code is as follows:

#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]

Note that Python2.x and Python3.x are not compatible with filter/map processing. In Python2 .x directly returns a list.

In Python3.x, an iterable object is returned, such as 9511eb37ecd99d90ad6638231e83323d, and the following string of numbers is the object reference address.

You can use list(words) conversion.

The above is the detailed content of Python filters out a sensitive word in a string array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn