杰瑞科技汇

Python filter与lambda如何高效协同工作?

Of course! Let's break down Python's filter() function and its common partner, lambda. This is a powerful and elegant combination for working with data.

Python filter与lambda如何高效协同工作?-图1
(图片来源网络,侵删)

The Big Picture: What is filter()?

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

In simpler terms, it lets you:

  1. Take a list (or any iterable) of items.
  2. Run each item through a "test" (a function).
  3. Keep only the items that pass the test (where the function returns True).
  4. Return a new, filtered object.

The Syntax

filter(function, iterable)
  • function: A function that tests each element. It must return True or False. If None, it will simply filter out "falsey" values.
  • iterable: The list, tuple, set, or any other collection you want to filter.

The Problem: A Standard filter() Example

Let's say we have a list of numbers and we want to keep only the even ones. We can do this by first defining a standard function and then using it with filter().

# 1. Our data
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 2. A standard function to check if a number is even
def is_even(num):
    """Returns True if the number is even, False otherwise."""
    return num % 2 == 0
# 3. Use filter() to get an iterator of even numbers
#    The result is a filter object, which is an iterator
even_numbers_iterator = filter(is_even, numbers)
# 4. Convert the iterator to a list to see the results
even_numbers_list = list(even_numbers_iterator)
print(f"Original list: {numbers}")
print(f"Filtered list: {even_numbers_list}")

Output:

Python filter与lambda如何高效协同工作?-图2
(图片来源网络,侵删)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Filtered list: [2, 4, 6, 8, 10]

This works perfectly, but it's a bit verbose. For a simple, one-time test, writing a whole def function feels overkill. This is where lambda shines.


The Solution: Combining filter() and lambda

A lambda function is a small, anonymous function defined with the lambda keyword. You can think of it as a disposable, one-line function.

Syntax of lambda:

lambda arguments: expression
  • arguments: Input(s) to the function.
  • expression: A single expression that is evaluated and returned. It cannot contain statements like if/else blocks or loops (though ternary expressions are fine).

How to use lambda with filter

We can replace our is_even function with a lambda that does the exact same thing.

Python filter与lambda如何高效协同工作?-图3
(图片来源网络,侵删)
# 1. Our data
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 2. Use filter() with a lambda function
#    The lambda takes one argument (num) and returns the result of num % 2 == 0
even_numbers_iterator = filter(lambda num: num % 2 == 0, numbers)
# 3. Convert to a list
even_numbers_list = list(even_numbers_iterator)
print(f"Original list: {numbers}")
print(f"Filtered list: {even_numbers_list}")

Output:

Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Filtered list: [2, 4, 6, 8, 10]

The result is identical, but the code is much more concise. We've defined the test directly inside the filter() call.


Key Characteristics and Important Details

filter() Returns an Iterator, Not a List

This is a crucial point. The filter() function doesn't create a new list immediately. It returns a filter object, which is an iterator. An iterator is an object that yields items one at a time. This is very memory-efficient, especially for large datasets.

You must convert it to a list (or tuple, etc.) to see all the values at once.

numbers = [1, 2, 3, 4]
# The result is a filter object
filtered = filter(lambda x: x > 2, numbers)
print(type(filtered)) # <class 'filter'>
# You can loop over the iterator directly
print("Looping over the iterator:")
for item in filtered:
    print(item)
# But if you try to convert it to a list now, it will be empty!
# Why? The iterator has been exhausted by the for loop.
print(list(filtered)) # Output: []

Using filter() with None

If you pass None as the function, filter() will remove any elements from the iterable that are considered "falsey".

In Python, the following values are considered falsey:

  • False
  • None
  • Zero of any numeric type (0, 0, 0j)
  • Empty sequences and collections (, , [], , set())
mixed_data = [0, 1, False, True, '', 'hello', [], [1, 2], None]
# Filter out all falsey values
truthy_values = filter(None, mixed_data)
print(list(truthy_values))

Output:

[1, True, 'hello', [1, 2]]

Notice how 0, False, , [], and None were all removed.


More Examples with filter and lambda

Example 1: Filtering Strings by Length

Keep only the words that have more than 3 letters.

words = ["apple", "cat", "dog", "banana", "a", "hi"]
long_words = list(filter(lambda word: len(word) > 3, words))
print(long_words) # Output: ['apple', 'banana']

Example 2: Filtering a List of Dictionaries

Get a list of dictionaries where the 'age' is 30 or older.

users = [
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 30},
    {'name': 'Charlie', 'age': 35},
    {'name': 'David', 'age': 28}
]
adults = list(filter(lambda user: user['age'] >= 30, users))
print(adults)

Output:

[{'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]

Modern Python: List Comprehensions

While filter + lambda is classic and powerful, modern Python often favors list comprehensions (or generator expressions) for their readability and performance.

A list comprehension does the same job as filter() + lambda but is often considered more "Pythonic."

Let's rewrite our previous examples using list comprehensions.

Original:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda num: num % 2 == 0, numbers))

List Comprehension:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [num for num in numbers if num % 2 == 0]

Readability: The list comprehension reads almost like plain English: "create a list of num for each num in numbers if num is even."

Performance: For simple cases, list comprehensions are generally faster than filter() + lambda because they are optimized for creating lists.

When to Use Which?

Method Pros Cons Best For
filter() + lambda - Classic, functional style.
- Good for simple, one-off tests.
- Works with any iterable, not just lists.
- Returns an iterator, can be confusing for beginners.
- Can be less readable for complex logic.
Simple filtering tasks, functional programming paradigms.
List Comprehension - Very readable and "Pythonic".
- Generally faster for creating lists.
- More flexible (can transform elements, not just filter).
- Syntax can get complex for very nested conditions. Most common use cases for filtering and transforming lists.

Conclusion:

Learn both! Understanding filter() and lambda is essential for reading older code

分享:
扫描分享到社交APP
上一篇
下一篇