Python cookbook (data structure and algorithm) is used to filter and extract elements in a sequence.
This article describes how to filter and extract elements in a sequence using Python. We will share this with you for your reference. The details are as follows:
Problem:Extracts values from a sequence or deletes the sequence according to certain criteria.
Solution:List derivation, generator expression, use built-infilter()
Function
1. List derivation method:There is a potential drawback. If the input data is very large, it may produce a huge result. considering this problem, we recommend that you select a generator expression.
# Examples of different ways to filter datamylist = [1, 4,-5, 10,-7, 2, 3,-1] print ('mylist = ', mylist) # use the list derivation formula pos = [n for n in mylist if n> 0] print ('positive number: ', pos) neg = [n for n in mylist if n <0] print ('negative: ', neg)
Running result:
Mylist = [1, 4,-5, 10,-7, 2, 3,-1] positive number: [1, 4, 10, 2, 3] negative number: [-5,-7,-1]
2. Generator expression method:
Mylist = [1, 4,-5, 10,-7, 2, 3,-1] print ('mylist = ', mylist) # Use the generator expression pos = (n for n in mylist if n> 0) print ('generator is ', pos) for x in pos: print (x)
Running result:
Mylist = [1, 4,-5, 10,-7, 2, 3,-1] generator: <generator object <genexpr> at 0x02421FD0> 141023
3. If the filtering criteria cannot be expressed simply in the list Derivation or generator expression, for example, the filtering process involves some exception handling or more complex details, you can consider placing the code that processes the filtering logic into a separate function, and then use the built-in filter () function for processing.
Values = ['1', '2', '-3','-', 'n'/A', '4', '5 ', '%'] def is_int (val): # put the code that processes the filtering logic in a separate function try: x = int (val) return True limit t ValueError: return Falseivals = list (filter (is_int, values) # Use filter (func, list) to filter print (ivals)
Running result:
['1', '2', '-3', '4', '5']
filter(func,list)
An iterator will be created. If you want to list the results, you need to uselist()
Convert the result to a list.
Supplement:
Replace new values with values that do not meet the criteria, rather than discard them. You can easily move the filtering conditions to a condition expression.
# Negative values clipped to 0neg_clip = [n if n> 0 else 0 for n in mylist] print (replace 'negative number with 0, result: ', neg_clip) # Positive values clipped to 0pos_clip = [n if n <0 else 0 for n in mylist] print ('positive number with 0, result: ', pos_clip )'''
Running result:
Mylist = [1, 4,-5, 10,-7, 2, 3,-1] The negative number is replaced with 0. Results: [1, 4, 0, 10, 0, 2, 3, 0] replace positive numbers with 0. Result: [0, 0,-5, 0,-7, 0, 0,-1]
Recommended toolsitertools.compress()
It accepts an iteratable object and a Boolean selector sequence as input.
It is useful if you want to apply the filtering results of a sequence to another related sequence.
# Use the filtering tool itertools. compress () addresses = ['2014 N clark', '2014 N clark', '2014 E 58TH ', '2014 N clark', '2014 n ravenswood ', '2014 W addison', '2014 N broadway', '2014 w granville ',] counts = [0, 3, 10, 4, 1, 7, 6, 1] from itertools import compressmore5 = [n> 5 for n in counts] a = list (compress (addresses, more5) print ()
Running result:
['5800 E 58TH', '1060 W ADDISON', '4801 N BROADWAY']
The key here is to first create a Boolean sequence to indicate which element can meet our conditions. Thencompress()
The function selects the corresponding element that satisfies the Boolean value of True.
Samefilter()
Functions are the same. Normallycompress()
The function returns an iterator. To return the list, uselist()
Convert the result to a list.
(The code is from Python Cookbook.)