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

Python filters out a sensitive word in a string array

伊谢尔伦
Release: 2017-04-29 10:10:51
Original
4031 people have browsed it

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]
Copy after login

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 , 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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template