map和lambda
同事问我python里,比如一个列表:
a = ['1', '2', '3']
如何变成:
b = ['1x', '2x', '3x']
好吧,果断不知道…原来pthon中有map函数,查看帮助文档:
map(...)
map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
该函数可以对每一个元素都进行function处理,并返回一个列表。
因此和lambda一起使用就能很方便地达到目的,完整代码如下:
>>> a = ['1', '2', '3']
>>> b = map(lambda a : a +'x' , a)
>>> b
['1x', '2x', '3x']
再来说说lambda函数,lambda函数类似于定义一个函数,但是没有return,方便简洁。例如上述的代码,等同于定义一个函数,有一变量a,与字符x做连接。利用lambda函数甚至不用写函数名,如:
>>> (lambda x : x+3)(2)
5
真是太方便啦!但是可阅读性有点下降哦~
map和lambda的更多相关文章
- filter,map,reduce,lambda(python3)
1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- 列表生成式+过滤器(filter)+映射(map)+lambda总结
这些都是python的特色,不仅强大,而且好用,配合起来使用更是无敌. 零.lambda lambda用于产生一个匿名表达式,组成部分为:lambda + ‘函数表达式’ ‘函数表达式’由一个冒号加上 ...
- python六剑客:map()、lambda()、filter()、reduce()、推导类表、切片
一:map():映射 map()有两个参数,一个函数,一个序列,序列中每一个元素都会做为参数传给前边的函数,然后生成新的列表, 第二个参数必须用一个序列:元祖,列表,字符串 >>> ...
- map/reduce+lambda让程序简单化
map()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. 也就是 map(f,[x,x,x, ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- Python3基础 map 与 lambda表达式配合 将指定系列元素乘2
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- 【python深入】map/reduce/lambda 内置函数的使用
python中的内置函数里面,有map和reduce两个方法,这两个方法可以非常好的去做一些事情,但是之前都没有用过,下面是关于这两个方法的介绍: 一.map相关 map()会根据提供的函数对指定的序 ...
- python 3.6.5 map() max() lambda匿名函数
python 3.6.5 sample: map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 functi ...
随机推荐
- SQL Server阻止了对组件xp_cmdshell过程的解决方案 分类: SQL Server 2015-03-05 08:31 305人阅读 评论(0) 收藏
SQL Server阻止了对组件xp_cmdshell过程的解决方案 错误描述:SQL Server阻止了对组件'xp_cmdshell'的过程'sys.xp_cmdshell'的访问.因为此组件已作 ...
- ng-if与ng-show、ng-hide指令的区别和注意事项
http://blog.csdn.net/aitangyong/article/details/44701769
- Boa服务器在ARM+Linux上的移植
下面给大家介绍一下Boa服务器移植的具体操作步骤,希望能够有帮助. 环境 主机:ubuntu8.10 交叉工具链:gcc-3.4.5-glibc-2.3.6 ...
- [转]Android中dp,px,sp概念梳理以及如何做到屏幕适配
http://blog.csdn.net/jiangwei0910410003/article/details/40509571 今天又开始我的App开发,因为之前一直做的是SDK,所以涉及到界面UI ...
- shell 验证ip
#!/bin/bash function isIp(){ IP=$ ];then echo "Wrong IP!" exit else a=`echo $IP | awk -F . ...
- Qt 学习之路 :Qt Quick Controls
自 QML 第一次发布已经过去一年多的时间,但在企业应用领域,QML 一直没有能够占据一定地位.很大一部分原因是,QML 缺少一些在企业应用中亟需的组件,比如按钮.菜单等.虽然移动领域,这些组件已经变 ...
- [转] Linux strace 简介
http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html 简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在L ...
- 权限系统与RBAC模型概述
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3793894.html ...
- Simple screenshot that explains the singleton invocation.
Here is the code: /* Some class,such as a config file,need to be only one.So we need to control the ...
- classpath and path.
simply talk about the <path> and the <classpath> in java development. when the <path& ...