详解python函数之map,Filter,Reduce

冬强酱_2017

冬强酱_2017

2018-04-02

2179人浏览

原创

本篇文章给大家分享的是详解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

         

macOS UI 自动化技能包 —— 用 Python 控制你的 Mac
macOS UI 自动化技能包 —— 用 Python 控制你的 Mac

让 macOS 自动化像说话一样简单 —— 用代码控制你的 Mac,无需学习 AppleScript 系统要求 macOS 14+ (Sonoma) Python 3.9+ 需要开启:辅助功能 + 屏幕录制权限

下载

Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

reduce python

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

2026.10.28

80

16

Vite 如何搭配 Vue.js
Vite 如何搭配 Vue.js

Vite搭配Vue.js专题汇集创建Vue 3项目、配置开发环境、路径别名、Vue插件、代理、多环境构建与调试的已审核文章,帮助读者完成从初始化到日常开发的配置。

2026.10.28

60

15

Vite 项目目录怎么看
Vite 项目目录怎么看

Vite项目目录专题汇集入口文件、src源码、public静态资源、路径别名、项目根目录和构建输出目录相关的已审核文章,帮助读者理解各文件夹的用途与引用方式。

2026.10.28

200

10

Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

2026.10.22

140

11

Gradle Wrapper配置教程
Gradle Wrapper配置教程

本专题介绍Gradle Wrapper的基本作用,涵盖文件组成、生成方法、版本配置和任务调用,帮助你理解项目如何使用指定的Gradle版本,减少不同开发环境带来的构建差异。

2026.10.22

120

10

Golangci-lint检查器使用教程
Golangci-lint检查器使用教程

本专题整理Golangci-lint常用Linters的用途与配置方法,覆盖错误处理、无效代码、安全、复杂度、命名和依赖检查,帮助项目合理选规则。

2026.10.22

80

15

Gradle怎么构建Java项目
Gradle怎么构建Java项目

本专题介绍Gradle Java项目构建方法,涵盖Java插件配置、依赖声明、源码编译、测试执行和打包检查,帮助你理解各阶段的任务关系,逐步完成项目构建。

2026.10.22

60

10

PHP时间戳漏洞修复教程
PHP时间戳漏洞修复教程

本专题汇总PHP常见时间戳漏洞,包括2038年溢出、时区漂移、时间戳伪造、重放攻击、时间盲注与日志审计失效等,并给出升级64位环境、使用 DateTimeImmutable、统一UTC、严格超时校验、预处理绑定、hash_equals 比对等修复方案,帮助开发者快速排查和加固时间相关代码。

2026.10.22

80

25

Golangci-lint配置文件与规则设置
Golangci-lint配置文件与规则设置

本专题聚焦Golangci-lint YAML配置文件,讲清运行参数、规则启停、目录排除、问题过滤及严重级别设置,便于团队统一检查标准。

2026.10.22

40

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Gradle Kotlin DSL API 参考
Gradle Kotlin DSL API 参考

共0课时 | 0人学习

LLVM 语言参考手册
LLVM 语言参考手册

共0课时 | 0人学习