python之map、filter、reduce、lambda函数 转
python之map、filter、reduce、lambda函数
map
map函数根据提供的函数对指定的序列做映射,定义:
map(function, sequence[,sequence,...])--->list
例1

>>> map(lambda x:x+2, [1, 2, 3])
[3, 4, 5]
>>> map(lambda x:x+2, (1, 2, 3))
[3, 4, 5]
>>> map(lambda x:x+2, [1, 2], [1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)

最后这个例子说lambda函数需要传2个参数(因为后面的列表是2个)
例2
>>> map(lambda x,y:x+y, [1, 2], [1, 2])
[2, 4]
>>> map(lambda x,y:x+y, [1, 2], (1,2))
[2, 4]
例3

>>> a
[{'type': 2, 'ID': 1}, {'type': 4, 'ID': 2}, {'ID': 3}]
>>> map(lambda x:x['ID'], a)
[1, 2, 3]
>>> map(lambda x:x['type'], a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
KeyError: 'type'

例子说明,如果其中的一个键不存在({'ID':3}不存在type)会报错。
例4
上面例子中只给了lambda,还可以用普通的函数

>>> def func2(x, y):
... return x+y
...
>>> map(func2, [1, 2, 3], [3, 2, 1])
[4, 4, 4]
>>>
>>> def func1(x):
... return x**2
...
>>> map(func1, [1, 2, 3])
[1, 4, 9]

例5
如果没有给定,就类似于zip函数了

>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4])
[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4,5])
[(1, 1), (2, 2), (3, 3), (4, 4), (None, 5)]
>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4,5], [1, 2, 3])
[(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, None), (None, 5, None)]

不过与zip不同
>>> zip([1, 2, 3, 4], [1, 2, 3, 4,5], [1, 2, 3])
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
filter
filter函数对指定的序列进行过滤操作。定义:
filter(function or None, sequence) -> list, tuple, or string
例1
>>> filter(lambda x:x%2==1, [1, 2, 3])
[1, 3]
>>> filter(lambda x:x%2==1, (1, 2, 3))
(1, 3)
reduce
reduce函数会对参数序列中元素进行累积。定义:
reduce(function, sequence[, initial]) -> value
注:function必须是有2个参数的函数
例1
>>> reduce(lambda x, y:x+y, [1,2,3,4])
10
>>> reduce(lambda x, y:x+y, [1,2,3,4], 10)
20
如果没有initial参数,这么算:(((1+2)+3)+4)
如果有initial参数,这么算: ((((10+1)+2)+3)+4)
lambda
编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数。
举例对比(列表中的元素平方):

>>> map(lambda x:x*x, range(5))
[0, 1, 4, 9, 16] >>> def sq(x):
... return x * x
...
>>> map(sq, range(5))
[0, 1, 4, 9, 16]

map(lambda x:x*x, range(5))
python之map、filter、reduce、lambda函数 转的更多相关文章
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- python几个特别函数map filter reduce lambda
lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x): return x**2 print f(4) Python中使用lambda的话,写成这样 g = l ...
- Python中map,filter,reduce,zip的应用
事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- Python【map、reduce、filter】内置函数使用说明
题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
随机推荐
- 《奥威Power-BI智能分析报表制作方法》精彩回顾
年的最后一个月,一年又快过去.工作和学习都不能耽误,本周三奥威公开课又如约与大家见面咯!不知老师教的图文报表在课后你们都有练习吗?趁热打铁,我们现在再次来温习一下吧. 本期分享的内容:<奥威Po ...
- 产生0-9 A-Z a-z
>题目要求: >>产生26个大写字母 >>产生26个小写字母 >>产生0-9这10个阿拉伯数字 >程序实现: package cn.fury.test; ...
- 由FutureTask的get方法靠什么机制来阻塞引发的思考
1. FutureTask的get方法靠什么机制来阻塞 看其get方法源码: /** * @throws CancellationException {@inheritDoc} */ public V ...
- 突破软件试用期的"土方法"
机器上有个试用版的vs2008. 想能正常用,又木有去搜序列号.于是用python纠结了这么一段代码: import time import subprocess import os timeNow ...
- JAVA经典算法40题(1-20)
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律 ...
- 同事的游戏项目--Robocode-学习链接
Robocode机器人库学习链接:http://www.pudn.com/search_db.asp?keyword=Robocode 官网 :http://robocode.sourceforge. ...
- Chart 绘制,自带动画效果
package com.example.canvasdemo; import android.annotation.SuppressLint; import android.content.Conte ...
- BAT脚本打印空行的使用方法
@echo off echo= echo, echo; echo+ echo/ echo[ echo] echo: echo. echo\ pause 这十种方法可以分为三组,每组的效率依次递减. 至 ...
- eclipse远程debug
由于一般比较正规项目,都会有好几个版本,有时候在测试版本的时候,一切都好好的,然后提交到其他版本之后会有各种各样的问题,这个时候如果不能快速准确的定位到问题,那么我们就需要用 eclipse远程deb ...
- VIM 中鼠标选择不选中行号
VIM 中鼠标选择不选中行号 在Vim中,我们一般会使用 :set nu 打开行号开关. 但是打开行号后,有个弊端,那就是在用鼠标进行选择的时候,会将前面的行号也一起进行拷贝了.但是在gVim中进行选 ...