Map

map(function_to_apply, list_of_inputs)

设有以下代码:

>>> items = [1, 2, 3, 4, 5]
>>> squared = []
>>> for i in items:
... squared.append(i**2)
...
>>> squared
[1, 4, 9, 16, 25]

这段代码实现的功能用map函数可以两行完成:

items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

高级一点地,可以用一列函数作为输入:

def multiply(x):
return (x*x)
def add(x):
return (x+x) funcs = [multiply, add]
for i in range(5):
value = list(map(lambda x: x(i)+1, funcs))
print(value) >>>
[1, 1]
[2, 3]
[5, 5]
[10, 7]
[17, 9]

Filter

Filter creates a list of elements for which a function returns true.

>>> number_list = range(-5,5)
>>> less_than_zero = list(filter(lambda x: x<0, number_list))
>>> print(less_than_zero)
[-5, -4, -3, -2, -1]

经过_filter_函数_过滤_,条件判断为真的结果的存入list。另外值得注意的地,_filter_是_built-in_函数,运行速度更快。

Reduce

Reduce is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list.

>>> from functools import reduce
>>> product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
>>> product
24

Map、Filter和Reduce函数(Python)的更多相关文章

  1. python3的map(),filter()和reduce()函数总结

    这三个都是内置的常用高阶函数(Higher-order function),用法如下: map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把 ...

  2. Python Map, Filter and Reduce

    所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...

  3. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

  4. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  5. Map, filter and reduce

    To add up all the numbers in a list, you can use a loop like this: Total is initialized to 0. Each t ...

  6. Python的map、filter、reduce函数 [转]

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

  7. python迭代和解析(3):range、map、zip、filter和reduce函数

    解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html range range()是一个内置函数,它返回一个数字序列,功能和Li ...

  8. python的高阶函数(map,filter,sorted,reduce)

    高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...

  9. python中的map、filter、reduce函数

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

  10. python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数

    函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...

随机推荐

  1. linux开机启动jar

    一.使用系统文件rc.local 启动命令可添加在/etc/rc.local(链接地址为/etc/rc.d/rc.local)中即可开机启动,不建议使用此种方法. 二.自定义启动脚本 1.新建启动脚本 ...

  2. 如何在某个apps包下面中创建APP

  3. HttpClient之HttpContext使用

    Multiple request sequences that represent a logically related session should be executed with the sa ...

  4. JMeter简单使用

    JMeter是apache公司基于java开发的一款开源压力测试工具.因为它是java开发的,所以运行的时候必须要安装jdk才可以:Jmeter是免安装的,所以拿到安装包后直接解压就可以使用了,它也是 ...

  5. iOS 常用代码之 UICollectionView

    记一下 不用每次都从0开始写,人生苦短 ,省点时间给自己 之前必须完成相关注册: . cell . 头部和尾部 [self.hotAndHistoryCollectionV registerNib:[ ...

  6. Leetcode_897. Increasing Order Search Tree

    题目:https://leetcode.com/problems/increasing-order-search-tree/ 题意: 将一棵二叉搜索树,重排为一棵递增的二叉排序树. 解法1: rson ...

  7. Bugku | Easy_Re

    学到一个函数: _mm_storeu_si128((__m128i *)&v5, _mm_loadu_si128((const __m128i *)&xmmword_413E34)); ...

  8. idea中git stash--解决pull冲突或切换分支问题

    场景1:pull代码时提示冲突,本地代码和远程代码有冲突 场景2:当切换分支时,当前分支的代码又不想commit 这两种场景可以使用git stash来解决,将当前未commit的代码暂存起来. 操作 ...

  9. 科普:std::sort干了什么

    std::sort算是STL中对OIer比较友好的函数了,但你有想过sort是如何保证它的高速且稳定吗? 正文 我们首先来到第一层:sort函数 template<typename _Rando ...

  10. 【刷题笔记】LeetCode 48. Rotate Image

    题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...