Python reduce / map / filter 函数区别

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门

       Python 中 reduce / map / filter 三个函数很容易搞混淆,虽然利用函数对迭代器或者序列中的元素操作,但是适用的场景却各不相同;


一.map 函数

       map 函数特点:对可迭代器或者序列中的每个元素进行相同的操作(例如每个元素+1等等),并返回迭代器或者列表,示例如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函数区别.py
@Time:2021/05/18 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""

def func1(x):
    # 将每一个元素计算平方值
    print("x=%d x*x=%d"%(x,x*x))
    return x*x

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    #方法一:
    value = map(func1,list1) #返回map对象,可以强制转为list列表
    print(list(value))

    print("***"*20)
    #方法二:
    value = map(lambda x:x*x, list1)  #返回map对象,可以强制转为list列表
    print(list(value))


'''
输出结果:

x=1 x*x=1
x=2 x*x=4
x=3 x*x=9
x=4 x*x=16
x=5 x*x=25
[1, 4, 9, 16, 25]
************************************************************
[1, 4, 9, 16, 25]
'''

       值得注意的是:map函数返回值是迭代器,注意返回的结果只能迭代一次,如果需要多次使用请提前保存结果并处理,例如:

def func1(x):
    # 将每一个元素计算平方值
    # print("x=%d x*x=%d"%(x,x*x))
    return x*x

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = map(func1,list1) #返回map对象,可以强制转为list列表
    print(list(value))
    print(list(value))

'''
输出:

[1, 4, 9, 16, 25]
[]
'''

       很懵逼是不是?明明没什么错误,为什么第二次输出就是空列表呢?因为map函数返回的迭代器只能迭代一次,解决办法:在获取结果的时候强转为 list列表 即可,实例如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函数区别.py
@Time:2021/05/18 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""

def func1(x):
    # 将每一个元素计算平方值
    # print("x=%d x*x=%d"%(x,x*x))
    return x*x

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = list(map(func1,list1)) #返回map对象,可以强制转为list列表
    print(list(value))
    print(list(value))

'''
输出:

[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
'''

二.reduce函数

       reduce 函数特点:从左到右对一个序列的项累计地应用有两个参数的函数,以此合并序列到一个单一值(例如累加或累乘列表元素等等),返回最终的计算结果,是一个值,示例如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函数区别.py
@Time:2021/05/18 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""

#python3在使用reduce函数时需要导入模块

from functools import reduce # 导入模块

def func1(x,y):
    # 把上一次计算的结果作为下一次的计算的输入
    print("x=%d y=%d x*y=%d"%(x,y,x*y))
    return x*y

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    #方法一:
    value = reduce(func1,list1) #等价 1*2*3*4*5 = 120
    print(value)
    print(type(value))

    print("***"*20)
    #方法二:
    value = reduce(lambda x,y:x*y, list1)  # 等价 1*2*3*4*5 = 120
    print(value)
    print(type(value))

'''
输出结果:

x=1 y=2 x*y=2
x=2 y=3 x*y=6
x=6 y=4 x*y=24
x=24 y=5 x*y=120
120
<class 'int'>
************************************************************
120
<class 'int'>
'''

三.filter函数

       filter 函数特点:对可迭代对象中的元素按照特定的条件进行筛选(例如筛选列表中所有的偶数等等),示例如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函数区别.py
@Time:2021/05/18 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""


lis=[0,1,2,3,4,5,6]

#定义筛选偶数的普通函数
def func4(x):
    return x%2==0

#第一种使用filter函数的方式---lambda
res5=filter(lambda x:x%2==0,lis)
print(list(res5))
print(list(res5))
print("***"*20)


#第二种使用filter函数的方式---普通函数二
res7=filter(func4,lis)
print(list(res7))
print(list(res7))

'''
输出结果:

[0, 2, 4, 6]
[]
************************************************************
[0, 2, 4, 6]
[]
'''

       懵逼?事实证明,filter 函数返回的结果也和 map 函数一样,只能迭代一次,解决方案和 map 的解决方案一样,在获取结果的时候强转为 list 列表 即可;


四.猜你喜欢

  1. Python 条件推导式
  2. Python 列表推导式
  3. Python 字典推导式
  4. Python 不定长参数 *argc/**kargcs
  5. Python 匿名函数 lambda
  6. Python return 逻辑判断表达式
  7. Python is 和 == 区别
  8. Python 可变数据类型和不可变数据类型
  9. Python 浅拷贝和深拷贝
  10. Python 异常处理
  11. Python 线程创建和传参
  12. Python 线程互斥锁 Lock
  13. Python 线程时间 Event
  14. Python 线程条件变量 Condition
  15. Python 线程定时器 Timer
  16. Python 线程信号量 Semaphore
  17. Python 线程障碍对象 Barrier
  18. Python 线程队列 Queue – FIFO
  19. Python 线程队列 LifoQueue – LIFO
  20. Python 线程优先队列 PriorityQueue
  21. Python 线程池 ThreadPoolExecutor(一)
  22. Python 线程池 ThreadPoolExecutor(二)
  23. Python 进程 Process 模块
  24. Python 进程 Process 与线程 threading 区别
  25. Python 进程间通信 Queue / Pipe
  26. Python 进程池 multiprocessing.Pool
  27. Python GIL 锁

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容