Problem: Extracting values from a sequence or deleting a sequence based on some criteria
Solution: List derivation, generator expression, using the built-in filter () function
1, List derivation method: There is a potential disadvantage, if the input data is very large may produce a large result, considering the problem, it is recommended to choose the generator expression
#Examples of different ways to filter dataMyList= [1, 4,-5, 10,-7, 2, 3,-1]Print('mylist=', MyList)#using list derivationpos = [n forNinchMyListifn >0]Print('positive numbers are:', POS) neg= [n forNinchMyListifN <0]Print('negative numbers are:', neg)
>>> ================================ RESTART ================================>>> mylist = [1, 4, -5, ten,-7, 2, 3,-1] Positive numbers are: [1, 4, 2, 3], negative numbers are: [-5,-7,-1]
2. Generator Expression method:
mylist = [1, 4, -5, ten,-7, 2, 3, -1 print ( " mylist= ,mylist ' # pos = (n for n in mylist Span style= "color: #0000ff;" >if n > print ( '
>>> ================================ RESTART ================================>>> mylist = [1, 4, -5, ten,-7, 2, 3,-1<generator object <genexpr> at 0x02421fd0>141023
3, if the filter criteria can not be simply expressed in the list deduction or generator expression, such as the filtering process involves some exception handling or more complex details, you can consider the processing of the filter logic code into a separate function, and then use the built-in filter () function processing.
values=['1','2','-3','-','N /A','4','5','%']defIs_int (val):#put the code that handles the filtering logic in a separate function Try: x=Int (val)returnTrueexceptValueError:returnfalseivals=list (Filter (is_int,values))#Filter by using filter (func,list)Print(ivals)
>>> ================================ RESTART ================================>>> [' 1"2"-3"4 ' ' 5 ' ]
Filter (func,list) creates an iterator that uses list () to convert the result to a list if you want the result to be in the form of lists.
Add:
Instead of discarding values that do not meet the criteria with the new values, you can easily implement them by moving the filter criteria into a conditional expression.
#negative values clipped to 0Neg_clip = [nifn > 0Else0 forNinchMyList]Print('The negative number is replaced by 0, and the result:', Neg_clip)#Positive values clipped to 0Pos_clip = [nifN < 0Else0 forNinchMyList]Print('The positive number is replaced by 0, and the result:', Pos_clip)" "
>>> ================================ RESTART ================================>>> mylist = [1, 4, -5, ten,-7, 2, 3,-1] Negative number is replaced by 0, result: [1, 4, 0, 0, 2, 3, -5 0, 7,-0, 0, 1,]
The recommended tool, Itertools.compress (), takes an iterative object and a boolean selector sequence as input.
This is useful if you want to apply the filter results of a sequence to another related sequence.
#using the Filter Tool itertools.compress ()addresses= [ '5412 N CLARK', '5148 N CLARK', '5800 E 58TH', '2122 N CLARK', '5645 N Ravenswood', '1060 W ADDISON', '4801 N BROADWAY', '1039 W GRANVILLE',]counts= [0, 3, 10, 4, 1, 7, 6, 1] fromItertoolsImportCompressmore5= [n > 5 forNinchcounts]a=list (Compress (addresses, MORE5))Print(a)
>>> ================================ RESTART ================================>>> [' 5800 E 58TH"1060 W ADDISON"4801 N BROADWAY ']
The key here is to first create a Boolean sequence that indicates which element satisfies our condition. the Compress () function then picks up the corresponding element that satisfies the Boolean value of True.
As with the filter () function, the compress () function normally returns an iterator that, if needed, uses list () to convert the result to a list.
"Python Cookbook" "Data Structure and algorithm" 16. Filter elements in a sequence