• Python3中的map()、reduce()、filter()


    Python3中的map()reduce()filter() 这3个一般是用于对序列进行操作的内置函数,它们经常需要与 匿名函数 lambda 联合起来使用,我们今天就来学习下。

    map()

    map() 可以用于在函数中对指定序列做映射,返回值是一个迭代器,其使用语法如下:

    map(function, *iterables)
    

    上面的第一个参数 function 指一个函数,第二个参数 iterable 指一个或多个可迭代对象,在执行过程中,会对可迭代对象中的每一个元素调用 function 函数做计算,最后得到一个新的迭代器对象,而这个新的迭代器对象,会包含有每次调用 function 函数的返回值。

    • 只传入一个可迭代对象
    """计算列表中每个元素的三次方"""
    def demo_map(x):
        return x ** 3
    
    
    nums = [1, 2, 3, 4, 5]
    print(list(map(demo_map, nums)))  # 输出:[1, 8, 27, 64, 125]
    
    # 使用匿名函数
    print(list(map(lambda x: x ** 3, nums)))  # 输出:[1, 8, 27, 64, 125]
    
    • 传入多个可迭代对象
    """计算3个列表中对应下标元素的和"""
    def demo_map(x, y, z):
        return x + y + z
    
    
    nums1 = [1, 2, 3, 4, 5]
    nums2 = [11, 22, 33, 44, 55]
    nums3 = [100, 200, 300, 400, 500]
    print(list(map(demo_map, nums1, nums2, nums3)))  # 输出:[112, 224, 336, 448, 560]
    
    # 使用匿名函数
    print(list(map(lambda x, y, z: x + y + z, nums1, nums2, nums3)))  # 输出:[112, 224, 336, 448, 560]
    

    filter()

    filter() 可以用于过滤序列,过滤掉不符合条件的元素,返回值也是一个迭代器,其使用语法如下:

    filter(function or None, iterable)
    

    map() 函数类似,上面的第一个参数 function 指一个函数,第二个参数 iterable 指一个可迭代对象,执行后会得到一个包含每次调用 function 函数返回值的迭代器。

    """找出从 -5 到 5 中能被 4 整除的所有整数"""
    def demo_filter(x):
        return x % 4 == 0
    
    
    nums = range(-5, 6)
    print(list(nums))  # 输出:[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
    
    # 传入None,只返回true的值(0是False,所以被过滤掉了)
    print(list(filter(None, nums)))  # 输入:[-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]
    
    # 传入正常函数,过滤出 nums 中能被 4 整除的整数
    print(list(filter(demo_filter, nums)))  # 输出:[-4, 0, 4]
    
    # 使用匿名函数
    print(list(filter(lambda x: x % 4 == 0, nums)))  # 输出:[-4, 0, 4]
    

    针对 map()filter() 函数, 这里有 2 点需要注意:

    • map 中必须传入一个正常函数,而在 filter 函数中则可以传正常函数或者None,当传入None时,只返回可迭代对象中所有符合 true 的值
    • map 中支持传多个可迭代对象,而在 filter 函数中则只能传一个可迭代对象

    reduce()

    reduce() 可以用于对参数序列中的元素进行累积,返回的是一个值。

    在 Python3 中,reduce() 已被从全局名字空间里移除了,如果想要使用它,那么需通过引入 functools 模块来调用 reduce() 函数,其使用语法如下:

    from functools import reduce
    
    reduce(function, sequence[, initial])
    

    上面的第一个参数 function 指一个函数,并且该函数必须含有2个参数,第二个参数 sequence 指一个序列,第三个参数 initial 指初始值,默认是None。例如存在函数:reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]),它就相当于 ((((1+2)+3)+4)+5)

    """计算 1-100 所有整数之和"""
    from functools import reduce
    
    
    def demo_reduce(x, y):
        return x + y
    
    
    nums = range(1, 101)
    
    print(reduce(demo_reduce, nums))  # 输出:5050
    
    # 使用匿名函数
    print(reduce(lambda x, y: x + y, nums))  # 输出:5050
    
    # 设置初始值为 1000
    print(reduce(lambda x, y: x + y, nums, 1000))  # 输出:6050
    

    上面的 map()、reduce()、filter() 都是属于Python3中的高阶函数,它们最大的好处在于可以让代码更加简洁,当然,如果不使用它们,我们也可以通过其他方式来实现。

  • 相关阅读:
    c# 判断点在区域内,外
    数据库行转列的sql语句
    正则表达式 mac 地址 匹配
    js check (转)
    MessageBox 确认对话框
    获得 客户端信息(IP && Mac)
    根据 标识 自动编号
    行转列 demo
    dataset 中 datatable 关联查询
    ACM 进阶指南
  • 原文地址:https://www.cnblogs.com/wintest/p/16182852.html
Copyright © 2020-2023  润新知