The Python array conditional filter function USES the example

  • 2020-04-02 13:52:52
  • OfStack

Using the filter function, a conditional judgment function can be implemented.

For example, to filter out a sensitive word in the string array, the following code is shown:


#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 and return a list directly in python2.x.

Returns an iterable object in python 3.x, for example < The filter object at 0 x000000000251c978 > , is the object reference address.

You can use list(words) transformations.


Related articles: