登录  /  注册

详解python函数之map,Filter,Reduce

零到壹度
发布: 2018-04-02 10:13:35
原创
1693人浏览过

本篇文章给大家分享的是详解python函数之map,filter,reduce,内容挺不错的,希望可以帮助到有需要的朋友

1.map

Map会将一个函数映射到一个输入列表的所有元素上。这是它的规范:

规范

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))
登录后复制

大多数时候,我们使用匿名函数(lambdas)来配合map, 所以我在上面也是这么做的。 不仅用于一列表的输入, 我们甚至可以用于一列表的函数!

def multiply(x):
        return (x*x)def add(x):
        return (x+x)

funcs = [multiply, add]for i in range(5):
    value = map(lambda x: x(i), funcs)
    print(list(value))    
    # 译者注:上面print时,加了list转换,是为了python2/3的兼容性
    #        在python2中map直接返回列表,但在python3中返回迭代器
    #        因此为了兼容python3, 需要list转换一下
    # Output:
    # [0, 0]
    # [1, 2]
    # [4, 4]
    # [9, 6]
    # [16, 8]
登录后复制

2.Filter

顾名思义,filter过滤列表中的元素,并且返回一个由所有符合要求的元素所构成的列表,符合要求即函数映射到该元素时返回值为True. 这里是一个简短的例子:

number_list = range(-5, 5)
less_than_zero = filter(lambda x: x <p style="font-size:16px;line-height:24px;margin-top:0px;margin-bottom:24px;color:rgb(64,64,64);font-family:Lato, 'proxima-nova', 'Helvetica Neue', Arial, sans-serif;background-color:rgb(252,252,252);">这个<code style="font-family:Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace;font-size:12px;white-space:pre;border:1px solid rgb(225,228,229);padding:0px 5px;color:rgb(231,76,60);background:rgb(255,255,255);">filter</code>类似于一个<code style="font-family:Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace;font-size:12px;white-space:pre;border:1px solid rgb(225,228,229);padding:0px 5px;color:rgb(231,76,60);background:rgb(255,255,255);">for</code>循环,但它是一个内置函数,并且更快。</p><p style="font-size:16px;line-height:24px;margin-top:0px;margin-bottom:24px;color:rgb(64,64,64);font-family:Lato, 'proxima-nova', 'Helvetica Neue', Arial, sans-serif;background-color:rgb(252,252,252);">注意:如果<code style="font-family:Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace;font-size:12px;white-space:pre;border:1px solid rgb(225,228,229);padding:0px 5px;color:rgb(231,76,60);background-color:rgb(255,255,255);">map</code>和<code style="font-family:Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace;font-size:12px;white-space:pre;border:1px solid rgb(225,228,229);padding:0px 5px;color:rgb(231,76,60);background-color:rgb(255,255,255);">filter</code>对你来说看起来并不优雅的话,那么你可以看看另外一章:列表/字典/元组推导式<em>。</em></p><p class="section"><br></p><h1 style="font-size:25.2px;margin-top:0px;font-family:'Roboto Slab', 'ff-tisa-web-pro', Georgia, Arial, sans-serif;"><code style="font-family:Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace;font-size:18.9px;white-space:pre;border:1px solid rgb(225,228,229);padding:0px 5px;color:rgb(231,76,60);background-color:rgb(255,255,255);">3.Reduce</code></h1><p style="font-size:16px;line-height:24px;margin-top:0px;margin-bottom:24px;">当需要对一个列表进行一些计算并返回结果时,<code style="font-family:Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace;font-size:12px;white-space:pre;border:1px solid rgb(225,228,229);padding:0px 5px;color:rgb(231,76,60);background:rgb(255,255,255);">Reduce</code> 是个非常有用的函数。举个例子,当你需要计算一个整数列表的乘积时。</p><p style="font-size:16px;line-height:24px;margin-top:0px;margin-bottom:24px;">通常在 python 中你可能会使用基本的 for 循环来完成这个任务。</p><p style="font-size:16px;line-height:24px;margin-top:0px;margin-bottom:24px;">现在我们来试试 reduce:</p><pre style="font-family:monospace, serif;" class="brush:php;toolbar:false;">from functools import reduce
product = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )
# Output: 24
登录后复制

         

以上就是详解python函数之map,Filter,Reduce的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号