Python 中 必须掌握的 20 个核心函数——filter()函数

filter()是Python中的高阶函数,用于过滤可迭代对象中的元素,返回一个由满足条件的元素组成的迭代器。它是函数式编程中的重大工具。

一、filter()的基本用法

1.1 方法签名

filter(function, iterable)
  • function:过滤函数,返回True或False(如果为None,则过滤掉假值)
  • iterable:要过滤的可迭代对象
  • 返回:过滤后的迭代器

1.2 基础示例

# 过滤偶数
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))  # [2, 4, 6, 8, 10]

# 过滤非空字符串
words = ["hello", "", "world", " ", "python", None, "filter"]
non_empty = filter(lambda x: x and x.strip(), words)
print(list(non_empty))  # ['hello', 'world', 'python', 'filter']

1.3 使用None作为过滤函数

# 过滤掉所有假值(False, None, 0, "", [], {}等)
values = [0, 1, False, True, "", "hello", [], [1, 2], None]
truthy_values = filter(None, values)
print(list(truthy_values))  # [1, True, 'hello', [1, 2]]

二、filter()与列表推导式的对比

2.1 功能等价性

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# 使用filter()
even_filter = filter(lambda x: x % 2 == 0, numbers)

# 使用列表推导式
even_lc = [x for x in numbers if x % 2 == 0]

print(list(even_filter))  # [2, 4, 6, 8, 10]
print(even_lc)            # [2, 4, 6, 8, 10]

2.2 性能比较

import timeit

numbers = list(range(10000))

# filter()性能测试
def test_filter():
    return list(filter(lambda x: x % 2 == 0, numbers))

# 列表推导式性能测试
def test_list_comprehension():
    return [x for x in numbers if x % 2 == 0]

print("filter():", timeit.timeit(test_filter, number=100))
print("列表推导式:", timeit.timeit(test_list_comprehension, number=100))
# 一般列表推导式稍快,但差异不大

2.3 选择提议

  • 简单过滤:列表推导式更直观
  • 已有判断函数:filter()更简洁
  • 复杂条件:列表推导式更灵活
  • 惰性求值:filter()返回迭代器,节省内存

三、实际应用场景

3.1 数据清洗

# 清理用户数据
raw_data = ["Alice", "", "Bob", " ", "Charlie", None, "David", "	
"]
clean_data = list(filter(lambda x: x and x.strip(), raw_data))
print(clean_data)  # ['Alice', 'Bob', 'Charlie', 'David']

# 过滤无效数字
def is_valid_number(value):
    try:
        float(value)
        return True
    except (ValueError, TypeError):
        return False

values = ["3.14", "2.71", "invalid", "1.618", None, "123"]
valid_numbers = filter(is_valid_number, values)
print(list(valid_numbers))  # ['3.14', '2.71', '1.618', '123']

3.2 复杂条件过滤

# 复合条件过滤
class Product:
    def __init__(self, name, price, in_stock):
        self.name = name
        self.price = price
        self.in_stock = in_stock

products = [
    Product("Laptop", 1000, True),
    Product("Mouse", 25, False),
    Product("Keyboard", 75, True),
    Product("Monitor", 300, True),
    Product("Webcam", 45, False)
]

# 过滤库存且价格低于100的产品
affordable_in_stock = filter(lambda p: p.in_stock and p.price < 100, products)
result = [p.name for p in affordable_in_stock]
print(result)  # ['Keyboard']

3.3 多层过滤

# 组合多个过滤条件
def create_filter_pipeline(*conditions):
    """创建过滤管道"""
    def combined_filter(item):
        return all(condition(item) for condition in conditions)
    return combined_filter

# 定义多个过滤条件
conditions = [
    lambda x: x > 0,           # 正数
    lambda x: x % 2 == 0,      # 偶数
    lambda x: x < 100,         # 小于100
    lambda x: len(str(x)) == 2 # 两位数
]

numbers = range(-50, 150)
pipeline_filter = filter(create_filter_pipeline(*conditions), numbers)
print(list(pipeline_filter))  # [10, 12, 14, ..., 98]

四、高级用法与技巧

4.1 使用内置函数和模块

import math

# 使用math模块函数
numbers = [1, 4, 9, 16, 25, 36, 49]
perfect_squares = filter(lambda x: math.isqrt(x)**2 == x, numbers)
print(list(perfect_squares))  # [1, 4, 9, 16, 25, 36, 49]

# 使用字符串方法
words = ["hello", "world", "python", "programming", "filter"]
long_words = filter(lambda x: len(x) > 5, words)
print(list(long_words))  # ['python', 'programming', 'filter']

4.2 与map()配合使用

import math

# 使用math模块函数
numbers = [1, 4, 9, 16, 25, 36, 49]
perfect_squares = filter(lambda x: math.isqrt(x)**2 == x, numbers)
print(list(perfect_squares))  # [1, 4, 9, 16, 25, 36, 49]

# 使用字符串方法
words = ["hello", "world", "python", "programming", "filter"]
long_words = filter(lambda x: len(x) > 5, words)
print(list(long_words))  # ['python', 'programming', 'filter']

4.2 与map()配合使用

from itertools import filterfalse

# filterfalse返回不满足条件的元素
numbers = [1, 2, 3, 4, 5, 6]
odd_numbers = filterfalse(lambda x: x % 2 == 0, numbers)
print(list(odd_numbers))  # [1, 3, 5]

# 相当于 filter(lambda x: not condition(x), iterable)

五、性能优化与内存管理

5.1 惰性求值优势

# 处理大型数据集时节省内存
def large_data_generator():
    for i in range(1000000):
        yield i

# 使用filter(惰性求值)
large_filter = filter(lambda x: x % 1000 == 0, large_data_generator())
# 此时还没有实际计算,不占用内存

# 需要时逐步处理
for i, value in enumerate(large_filter):
    if i >= 5:  # 只处理前5个
        break
    print(value)  # 0, 1000, 2000, 3000, 4000

5.2 生成器表达式对比

# filter vs 生成器表达式
numbers = range(1000000)

# filter
even_filter = filter(lambda x: x % 2 == 0, numbers)

# 生成器表达式
even_gen = (x for x in numbers if x % 2 == 0)

# 两者都是惰性的,内存友善

六、常见问题解答

6.1 filter()返回什么类型?

numbers = [1, 2, 3, 4, 5]
result = filter(lambda x: x % 2 == 0, numbers)
print(type(result))  # <class 'filter'>
# 返回filter对象(迭代器),不是列表

6.2 如何多次使用filter()结果?

numbers = [1, 2, 3, 4, 5]
even = filter(lambda x: x % 2 == 0, numbers)

# filter对象是迭代器,遍历一次后耗尽
list1 = list(even)  # [2, 4]
list2 = list(even)  # [] (已耗尽)

# 解决方案:转换为列表或重新创建
even_list = list(filter(lambda x: x % 2 == 0, numbers))  # 转换为列表
# 或者
even = list(filter(lambda x: x % 2 == 0, numbers))  # 直接存储列表

6.3 处理复杂数据结构

numbers = [1, 2, 3, 4, 5]
even = filter(lambda x: x % 2 == 0, numbers)

# filter对象是迭代器,遍历一次后耗尽
list1 = list(even)  # [2, 4]
list2 = list(even)  # [] (已耗尽)

# 解决方案:转换为列表或重新创建
even_list = list(filter(lambda x: x % 2 == 0, numbers))  # 转换为列表
# 或者
even = list(filter(lambda x: x % 2 == 0, numbers))  # 直接存储列表

6.3 处理复杂数据结构

class Validator:
    @staticmethod
    def is_valid_email(email):
        return email and "@" in email and "." in email.split("@")[-1]

emails = ["alice@example.com", "invalid", "bob@test", "charlie@domain.org"]
valid_emails = filter(Validator.is_valid_email, emails)
print(list(valid_emails))  # ['alice@example.com', 'charlie@domain.org']

七、总结最佳实践

  1. 简单过滤:优先使用列表推导式
  2. 已有判断函数:使用filter()更简洁
  3. 复杂条件:组合多个过滤函数
  4. 大数据集:利用filter()的惰性求值特性
  5. 代码可读性:为复杂条件命名或使用注释
# 综合示例:数据验证管道
def create_validation_pipeline(*validators):
    """创建数据验证管道"""
    def validate_item(item):
        return all(validator(item) for validator in validators)
    return validate_item

# 定义验证器
def is_positive(number):
    return number > 0

def is_even(number):
    return number % 2 == 0

def is_two_digit(number):
    return 10 <= number < 100

# 创建验证管道
validate_pipeline = create_validation_pipeline(is_positive, is_even, is_two_digit)

# 过滤数据
numbers = range(-50, 150)
filtered = filter(validate_pipeline, numbers)
print(list(filtered))  # [10, 12, 14, ..., 98]

filter()函数是Python函数式编程的重大工具,特别适合数据过滤和清洗场景。合理使用可以使代码更简洁、更高效,特别是在处理大型数据集时,其惰性求值特性可以显著节省内存。

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
欣然起行的头像 - 宋马
评论 抢沙发

请登录后发表评论

    暂无评论内容