SoFunction
Updated on 2024-11-13

How Python filters data with the filter function

I. Introduction to filter function

filter function is mainly used to filter data, filter out elements that do not meet the conditions, and return an iterator object, if you want to convert to a list list or tuple of metazoans, you can use the built-in function list () or built-in function tuple () to convert;

filter function receives two parameters, the first for the function, the second for the sequence, each element of the sequence as a parameter passed to the function for judgment, and then return True or False, and finally put the elements that return True to the new list, as if it were a sieve, sifting through the specified elements.

Grammar:

filter(function, iterable)

Parameters:

function - The name of the function;

iterable - A sequence or iterable object;

Return value: after filtering by function, the elements that return True will be saved in the iterator object, and finally return this iterator object (python2.0x version is to return the list list directly);

II. filter function to use

Simple use of functions

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:What can I do to help you?
@Blog(Personal Blog Address): 
@WeChat Official Account(WeChat Public):apes saypython
@Github:
 
@File:python_process_Pool.py
@Time:2020/1/14 21:25
 
@Motto:short steps don't make a big step,You can't make a river or an ocean without accumulating small streams.,The excitement of the program life needs to be built up persistently.!
"""
def check(i):
  # If even return True otherwise False
  return True if i%2 == 0 else False
 
if __name__ == "__main__":
 
  list1 =[1,2,3,4,5,6]
  result = filter(check,list1)
  print(result)
  print(type(result))
 
  # Convert the returned iterator to a listlist or tuple
  print(list(result))
  print(type(list(result)))

Output results:

<filter object at 0x0000015127BA7EB8>
<class 'filter'>
[2, 4, 6]
<class 'list'>

Functions used in conjunction with the anonymous function Lambda

def check_score(score):
  if score > 60:
    return True
  else:
    return False
 
if __name__ == "__main__":
 
  # List of accomplishments
  student_score = {"zhangsan":98,"lisi":58,"wangwu":67,"laowang":99,"xiaoxia":57}
 
  # Filter the list of grades greater than 60
  result = filter(lambda score:score > 60,student_score.values())
  # Equivalent to the above line of code
  # result = filter(check_score, student_score.values())
 
  print(result)
  print(type(result))
 
  # Convert the returned iterator to a listlist or tuple
  print(list(result))
  print(type(list(result)))

Output results:

<filter object at 0x000001B761F88FD0>
<class 'filter'>
[98, 67, 99]
<class 'list'>

Note: The filter function returns an iterator object, which often needs to be converted to a list or tuple before it can be used;

The python filter function is actually similar to the built-in function map(), which maps each element of an iterator or sequence to a specified function, and then returns the modified iterator object after the operation is complete;

This is the whole content of this article.