杰瑞科技汇

Python的def filter函数如何正确使用?

Of course! The filter() function is a built-in Python function that constructs an iterator from elements of an iterable for which a function returns True.

Python的def filter函数如何正确使用?-图1
(图片来源网络,侵删)

In simpler terms, it lets you "filter" items from a list (or any other iterable like a tuple, set, etc.) based on a condition you define.


The Basic Syntax

The syntax for filter() is:

filter(function, iterable)

Let's break that down:

  • function: This is a function that tests each element in the iterable. It should return True if the element should be included in the result, and False otherwise. This function is often called a "predicate".
  • iterable: This is any iterable object (like a list, tuple, set, etc.) whose elements you want to filter.

Important Return Value: filter() returns an iterator, not a list. An iterator is an object that yields one item at a time, which is very memory-efficient for large datasets. If you want to see the results as a list, you need to convert the iterator to a list using list().


How It Works: A Simple Example

Let's say you have a list of numbers and you only want the even numbers.

Using a Standard def Function

First, you define a function that checks if a number is even.

def is_even(num):
  """Returns True if a number is even, False otherwise."""
  return num % 2 == 0
# Our list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Use filter() to get only the even numbers
# The filter() object is an iterator, so we convert it to a list
even_numbers = list(filter(is_even, numbers))
print(even_numbers)
# Output: [2, 4, 6, 8, 10]

Step-by-step:

  1. filter(is_even, numbers) is called.
  2. filter() takes the first element from numbers (which is 1) and passes it to is_even(1).
  3. is_even(1) returns False (because 1 % 2 is 1, not 0). filter() discards 1.
  4. filter() takes the next element (2) and passes it to is_even(2).
  5. is_even(2) returns True. filter() keeps 2.
  6. This process continues for all elements in numbers.
  7. The result is an iterator that will yield 2, 4, 6, 8, 10 in sequence.
  8. list() consumes this iterator and creates a new list: [2, 4, 6, 8, 10].

The Modern Python Way: Lambda Functions

For simple, one-time-use functions like is_even, defining a full def function can be verbose. Python offers lambda functions, which are small, anonymous functions defined on a single line.

The syntax for a lambda is: lambda arguments: expression

Let's rewrite the previous example using a lambda:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Use a lambda function to check for even numbers
even_numbers = list(filter(lambda num: num % 2 == 0, numbers))
print(even_numbers)
# Output: [2, 4, 6, 8, 10]

This does the exact same thing as the previous example but is more concise and common in modern Python code. The lambda lambda num: num % 2 == 0 is a function that takes one argument (num) and returns the result of the expression num % 2 == 0.


Filtering with Different Data Types

filter() is not just for numbers! You can use it with any iterable and any condition.

Example: Filtering Strings

Let's filter a list of words to get only those that are longer than 4 characters.

words = ["apple", "banana", "kiwi", "strawberry", "fig"]
long_words = list(filter(lambda word: len(word) > 4, words))
print(long_words)
# Output: ['apple', 'banana', 'strawberry']

Example: Filtering Objects

Let's say you have a list of dictionaries (representing users) and you want to filter for only the active ones.

users = [
    {"name": "Alice", "active": True},
    {"name": "Bob", "active": False},
    {"name": "Charlie", "active": True},
    {"name": "David", "active": False},
]
# Filter for active users
active_users = list(filter(lambda user: user["active"], users))
print(active_users)
# Output: [{'name': 'Alice', 'active': True}, {'name': 'Charlie', 'active': True}]

Note: In Python, True is equivalent to 1 and False is equivalent to 0. So, user["active"] directly evaluates to True or False, making the lambda work perfectly.


What if the Function is None?

If you pass None as the first argument to filter(), the function defaults to the identity function. This means it simply returns the elements of the iterable that evaluate to True in a boolean context.

This is useful for removing "falsy" values from a list (like 0, False, None, , []).

mixed_values = [0, 1, "", "hello", False, True, None, 25, []]
# Filter out all falsy values
truthy_values = list(filter(None, mixed_values))
print(truthy_values)
# Output: [1, 'hello', True, 25]

filter() vs. List Comprehensions

You will often see filter() used alongside a lambda. However, for most use cases, a list comprehension is considered more "Pythonic" and readable.

Let's compare the two ways to get even numbers:

Method Code Readability
filter() with lambda list(filter(lambda x: x % 2 == 0, numbers)) Can be less readable, especially for beginners. The lambda syntax can be a bit dense.
List Comprehension [x for x in numbers if x % 2 == 0] Generally considered more readable and explicit. It clearly states: "give me x for each x in numbers if x % 2 == 0".

Recommendation:

  • Use list comprehensions for simple filtering tasks. They are faster and more readable.
  • Use filter() when you already have a named function that you want to apply as a predicate, or when you are working with very large datasets and want to benefit from the lazy evaluation of iterators before converting to a list.

Summary

Feature Description
Purpose To filter elements from an iterable based on a condition.
Syntax filter(function, iterable)
function A function that returns True or False. A lambda is often used for simple cases.
Return Value An iterator, not a list. Use list() to see the results.
Common Alternative List comprehensions ([x for x in iterable if condition]) are often preferred for readability.
分享:
扫描分享到社交APP
上一篇
下一篇