Python has a few functions that are useful for this sort of “functional programming”: map, filter, and
reduce. 4 (In Python 3.0, these are moved to the functools module.) The map and filter functions are not
really all that useful in current versions of Python, and you should probably use list comprehensions instead.
You can use map to pass all the elements of a sequence through a given function:
>>> map(str, range(10)) # Equivalent to [str(i) for i in range(10)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
You use filter to filter out items based on a Boolean function:
>>> def func(x):
... return x.isalnum()
...
>>> seq = ["foo", "x41", "?!", "***"]
>>> filter(func, seq)
['foo', 'x41']

For this example, using a list comprehension would mean you didn’t need to define the custom function:
>>> [x for x in seq if x.isalnum()]
['foo', 'x41']
Actually, there is a feature called lambda expressions, 5 which lets you define simple functions in-line
(primarily used with map, filter, and reduce):
>>> filter(lambda x: x.isalnum(), seq)
['foo', 'x41']
Isn’t the list comprehension more readable, though?
The reduce function cannot easily be replaced by list comprehensions, but you probably won’t need its
functionality all that often (if ever). It combines the first two elements of a sequence with a given function,
combines the result with the third element, and so on until the entire sequence has been processed and a sin-
gle result remains. For example, if you wanted to sum all the numbers of a sequence, you could use reduce
with lambda x, y: x+y (still using the same numbers): 6
>>> numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
>>> reduce(lambda x, y: x+y, numbers)
1161
Of course, here you could just as well have used the built-in function sum.

转载于:https://www.cnblogs.com/zhuweiblog/p/5185041.html

python map、filter、reduce相关推荐

  1. 如何用Map、Filter和Reduce替换Python For循环?

    2020-01-06 17:00:00 全文共2375字,预计学习时长7分钟 图源:Unsplash 屏幕前的你,瞅瞅看,你的代码是不是通篇都是For循环?是不是眯眼靠近屏幕才能看清自己的代码? 没错 ...

  2. python进阶(小白也能看懂)——Map、Filter、Reduce

    python进阶(小白也能看懂)--Map.Filter.Reduce 第三篇 Map.Filter.Reduce是python中常用的函数,使用这些函数能够给我们带来很多便捷. Map map(fu ...

  3. map for循环_如何用Map、Filter和Reduce替换Python For循环?

    全文共2375字,预计学习时长7分钟 图源:Unsplash 屏幕前的你,瞅瞅看,你的代码是不是通篇都是For循环?是不是眯眼靠近屏幕才能看清自己的代码? 没错,我就是这样的.(难以切齿) For循环 ...

  4. Python的map、filter、reduce函数

    Python的map.filter.reduce函数 map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq):  ...

  5. Python函数式编程-map()、zip()、filter()、reduce()、lambda()

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function, seq ...

  6. [转载] Python 内置函数 lambda、filter、map、reduce

    参考链接: Python lambda (匿名函数) | filter, map, reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python ...

  7. Python之高阶函数(abs、map、reduce、filter、lambda匿名函数)

    Python之高阶函数(abs.map.reduce.filter.lambda匿名函数) 什么是内置高阶函数 高阶函数:一个函数可以作为参数传给另外一个函数,或者一个函数的返回值为另外一个函数(若返 ...

  8. map 长度_Python实用教程系列——高阶函数Map、Filter、Reduce

    点击上方蓝色文字关注我们吧 有你想要的精彩 作者 | 那个百分十先生出品 | Python知识学堂 上次推文我们介绍了python中的<Logging日志模块>的相关知识,这次推文我们将学 ...

  9. [转载] Python---函数式编程(map()、filter()和reduce())总结

    参考链接: 带有示例的Python中的map(),filter()和reduce() (1)map()函数 map() 函数的基本语法格式为:map(function, iterable) 其中,fu ...

  10. Swift学习之map、flatMap、filter、reduce的使用

    Swift相比于Objective-C又一个重要的优点,它对函数式编程提供了很好的支持,Swift提供了map.filter.reduce这三个高阶函数作为对容器的支持. 1.map:对数组中的每一个 ...

最新文章

  1. 消息幂等(去重)通用解决方案
  2. Python学习笔记:面向对象高级编程(完)
  3. SQL server 统计分组经计
  4. Apache Camel 2.20发布–新增功能
  5. python获取某文件路径_Python获取当前文件路径
  6. fst java_java快速序列化库FST
  7. Centos7-Lvs+Keepalived架构实验详解
  8. NSUserDefaults使用
  9. Appium 屏幕页面滑动(swipe函数实现)
  10. oracle 的控制结构
  11. HTML URL编码
  12. 精灵图,雪碧图的应用
  13. git把一个分支上的某次提交同步到另一个分支上
  14. 3DMax如何通过光子图渲染
  15. 什么是全栈工程师 摘自360
  16. 01 linux 下tcl源码编译安装和tcl手册使用
  17. Docker搭建snipe-it全步骤
  18. 神奇的 ViewDragHelper,让你轻松定制拥有拖拽能力的 ViewGroup
  19. 优思学院|六西格玛设计方法IDDOV是什么?
  20. O32 对接 交易所报盘软件_沈阳企业“新”社保报盘软件(下载地址)!

热门文章

  1. 六级范文背诵50篇_清华附小校长:小学生日记写作训练,附经典范文50篇,家长收藏...
  2. jmeter(一)安装教程
  3. python队列是线程安全的吗_python – 为什么我的多进程队列看起来不是线程安全的?...
  4. find命令过滤 no such_运维老手常用的linux命令操作
  5. 带有下标的赋值维度不匹配_KDD 2019|?结构感知的神经网络对人才-组织匹配的影响...
  6. logo自动旋转 html,HTML/CSS3 Logo选择器
  7. mysql数据库 on 命令_MySql 数据库基础命令
  8. daterangepicker java_日期选择插件Date Range Picker
  9. c# groupbox大小_【已解决】C#中使得控件随着WinForm窗体的大小改变而自动变化
  10. php面向对象代码_PHP面向对象之抽象类详解(代码实例)