Python Method Implementation of How to Filter Elements in a Sequence

  • 2021-07-18 08:21:06
  • OfStack

This article brings us the content is about Python how to filter the elements in the sequence, there is a reference value, friends in need can refer to 1, I hope to help you.

1. Requirements

The sequence contains some data, and we need to extract the values or delete the sequence according to some criteria.

2. Solutions

Usually the easiest way to filter the data in a sequence is to use a list derivation.

For example:


myList=[1,4,-5,10,-7,2,3,-1]
print([n for n in myList if n>0])
print([n for n in myList if n<0])

Results:

[1, 4, 10, 2, 3]
[-5, -7, -1]

One potential disadvantage of using list derivation is that if the original input is very large, it may produce a huge result. If this is a problem you need to consider, you can use generator expressions to generate filter results iteratively, such as:


myList=[1,4,-5,10,-7,2,3,-1]
pos=(n for n in myList if n >0)
for x in pos:
  print(x)

Results:
1
4
10
2
3

Sometimes filtering criteria cannot be simply expressed in list derivation or generator expression. For example, suppose the filtering process involves exception handling or other complicated details. Based on the slave, you can put the code that handles the filtering logic into a separate function and then use the built-in filter () function, as shown in the following example:


values=['1','2','-3','-','4','N/A','5']
def is_int(val):
  try:
    x=int(val)
    return True
  except ValueError:
    return False
 
ivals=list(filter(is_int,values))
print(ivals)

Results:
['1', '2', '-3', '4', '5']

filter () creates an iterator, so if we want a list of results, make sure you add list (), as in the example.

3. Analysis

List derivation and generator expressions are usually the simplest and most direct way to filter data. In addition, they also have the ability to transform data at the same time. For example:


import math
myList=[1,4,-5,10,-7,2,3,-1]
print([math.sqrt(n) for n in myList if n>0])

Results:
[1.0, 2.0, 3.1622776601683795, 1.4142135623730951, 1.7320508075688772]

With regard to filtering data, there is a case where values that do not meet the criteria are replaced with new values instead of discarding them. For example. In addition to finding positive integers, we also want to replace values that do not meet the requirements within the specified range. Typically, this can be easily achieved by migrating the filter into a condition expression, as follows:


myList=[1,4,-5,10,-7,2,3,-1]
print([n if n>0 else 0 for n in myList])
print([n if n<0 else 0 for n in myList])

Results:
[1, 4, 0, 10, 0, 2, 3, 0]
[0, 0, -5, 0, -7, 0, 0, -1]

Another filter tool worth mentioning is itertools. compress (), which takes an iterable object and a Boolean selector sequence as input. When output, it gives all iterable object elements that are True in the corresponding Boolean selector. This is useful if you want to apply the filtering results of one sequence to another related sequence.

For example:


from itertools import compress
address=[
'5412 N CLARK1',
'5148 N CLARK2',
'5800 E CLARK3',
'2122 N CLARK4',
'5645 M CLARK5',
'1060 W CLARK6',
]
counts=[0,3,10,4,1,7]
# Build 1 List, which corresponds to count Value is greater than 5
more5=[n>5 for n in counts]
print(more5)
 
print(list(compress(address,more5)))

Results:
[False, False, True, False, False, True]
['5800 E CLARK3', '1060 W CLARK6']

The key here is to first create a Boolean sequence that represents which element satisfies our condition, and then the compress () function picks out the corresponding element that satisfies the Boolean value of True.

Like filter () function 1, compress () normally returns 1 iterator. Therefore, if necessary, you have to use list () to turn the results into a list.


Related articles: