所属网站分类: python基础 > 函数


作者:慧雅

原文链接: http://www.pythonheidong.com/blog/article/21/

来源:python黑洞网 www.pythonheidong.com

Map, Filter and Reduce

这三个功能有助于编程的提升。我们将逐一讨论它们并了解它们的用例。

Map

Map将函数应用于input_list中的所有项

map(function_to_apply, list_of_inputs)

大多数情况下,我们希望将所有列表元素逐个传递给函数,然后收集输出结果。例如:

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

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), funcs))
    print(value)

# Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]

Filter

顾名思义,filter创建一个函数返回true的元素列表。这是一个简短的例子:

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

# Output: [-5, -4, -3, -2, -1]

过滤器类似于for循环,但它是内置函数,速度更快。

注意:如果map和filter看起来不厉害,那么您可以阅读有关list/dict/tuple部分的内容。

reduce

Reduce是一个非常有用的函数,用于在列表上执行某些计算并返回结果。它将滚动计算应用于列表中的连续值对。例如,如果要计算整数列表的乘积。

因此,在python中执行此任务的正常方法是使用基本for循环:

product = 1
list = [1, 2, 3, 4]
for num in list:
    product = product * num

# product = 24

现在让我们尝试使用reduce:

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

# Output: 24
 

Python Map, Filter and Reduce的更多相关文章

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

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

  2. Map,Filter和Reduce

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

  3. 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 ...

  4. python map filter reduce的优化使用

    这篇讲下python中map.filter.reduce三个内置函数的使用方式,以及优化方法. map()函数 map()函数会根据提供的函数对指定序列做映射. 语法: map(function,it ...

  5. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

  6. Python之内建函数Map,Filter和Reduce

    Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...

  7. Python map filter reduce enumerate zip 的用法

    map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...

  8. js Array 中的 map, filter 和 reduce

    原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...

  9. python库函数Map, Filter and Reduce的用法

    python中有三个函数式编程极大的简化了程序的复杂性,这里就做一下讨论和记录. 一 Map:应用在链表输入所有元素的函数,它的格式如下所示: map(function_to_apply, list_ ...

随机推荐

  1. java多线程关键字volatile的使用

    java多线程关键字volatile的作用是表示多个线程对这个变量共享. 如果是只读的就可以直接用,写数据的时候要注意同步问题. 例子: package com.ming.thread.volatil ...

  2. PHPGGC学习----实践

    本文首发于先知:https://xz.aliyun.com/t/5450 PHPGGC学习----理论部分对PHPGGC工具的使用方法有了一个基本的了解,接下来需要利用实践环境进行一个实践操作,巩固一 ...

  3. Golang自带的http包的路由规则问题

    1.调用下面的方法开启一个http监听服务http.HandleFunc("/hello/", helloHandler)err := http.ListenAndServe(&q ...

  4. cd进入相关目录的命令

    今天不记得怎么进入Linux的根目录了,查询了下顺便复习下其他命令: 1.[root@localhost]#cd /usr 切换至根目录下的文件夹要加"/" 2.[root@loc ...

  5. 如何下载Oracle E-Business Suite (12.2.6) for Microsoft Windows x64 (64-bit)

    下载地址:https://edelivery.oracle.com/ 使用您的 Oracle 账户进行登录.如果您没有该账户, 请注册 Oracle 账户.     Oracle Software D ...

  6. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)用法

    如果已经启动了四个Activity:A,B,C和D.在D Activity里,我们要跳到B Activity,同时希望C finish掉,可以在startActivity(intent)里的inten ...

  7. mongodb 认证方式(version:3.0.4)

    之前一直在本机上跑,前段时间把后台架到云服务器上了,在settings里加上了username和password,希望同步的时候修改一下settings就能在本地测试.   因为云服务器端数据库连接需 ...

  8. sudo用户权限添加问题

    现象:通过visudo或者vim /etc/sudoers文件添加用户权限后,该用户测试时依然需要输入密码解决:查看/etc/passwd用户id可能重复并且重复的uid排在该用户上面

  9. 开发Maven插件

    Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...

  10. pysnmp使用

    install yum install python-pysnmp yum install python-pyasn1 or pip install pysnmp pip install pyasn1 ...