python: filter, map, reduce, lambda
filter built-in function
filter(f,sequence)
filter can apply the function f to each element of sequence. If return is true the element will be returned and re-organized to be a new sequence. The type of new sequence can be list/tuple/string, this depends on the input sequence type.
>>> l=[1,2,3,4,5]
>>> f = lambda x: True if x%2 == 0 else False
>>> filter(f,l)
[2, 4]
>>> t=(1,2,3,4,5)
>>> filter(f,t)
(2, 4)
map built-in function
map(f, sequence)
map will apply f to each element of sequence. The result will be returned to be a new list(Unless filter, the return will always a list).
>>> f= lambda y : y*2
>>> l=[1,2,3]
>>> t=(1,2,3)
>>> map(f,l)
[2, 4, 6]
>>> map(f,t)
[2, 4, 6]
reduce() built-in function
reduce(f, sequence, start_value)
reduce apply f to sequence by the order of the sequence. If there is a start_value, this one will be called first.
>>> l = [1,2,3,4,5,6,7,8,9]
>>> f = lambda x,y : x+y
>>> reduce(f,l)
45
>>> reduce(f,l, 20)
65
python: filter, map, reduce, 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: ...
- python filter map reduce
filter(function, iterable): Construct a list from those elements of iterable for which function retu ...
- Python中特殊函数和表达式 filter,map,reduce,lambda
1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
随机推荐
- python与shell通过微信企业号发送消息
python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...
- OpenMP入门教程(三)
承接前面两篇,这里直接逐一介绍和使用有关OpenMP的指令和函数 Directives 1.for 作用:for指令指定紧随其后的程序的循环的迭代必须由团队并行执行,只是假设已经建立了并行区域,否则它 ...
- DEALLOCATE - 删除一个准备好的查询
SYNOPSIS DEALLOCATE [ PREPARE ] plan_name DESCRIPTION 描述 DEALLOCATE 用于删除前面准备好的查询. 如果你没有明确 DEALLOCATE ...
- createdb - 创建一个新的 PostgreSQL 数据库
SYNOPSIS createdb [ option...] [ dbname] [ description] DESCRIPTION 描述 createdb 创建一个新的 PostgreSQL 数据 ...
- c++ 数组长度
数组长度求解 sizeof template <class T>int getArrayLen(T &array){ return (sizeof(array) / sizeof( ...
- mysql5.7 this is incompatible with sql_mode=only_full_group_by错误
解决办法: https://blog.csdn.net/qq_42175986/article/details/82384160 前言: 一.原理层面 这个错误发生在mysql 5.7 版本及以上版本 ...
- iOS 导航栏风格
IOS-导航栏风格 导航控制器可以用几种不同的风格来显示自身.默认风格就是标准的灰色外观.目前支持三种不同的风格. 风 格 描 述 UIBarStyleDefault 默认风格:灰色背景, ...
- Tab键可访问的下拉菜单demo
<ul id="navigationRegion"> <li token="1" class="index_on"> ...
- ssm 使用中的一些问题
1.问题:[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession [org.mybatis.spring.SqlSession ...
- python3.x Day4 内置方法,装饰器,生成器,迭代器
内置方法,就是python3提供的各种函数,可以认为是关键字,帮助进行一些列的牛x运算. abs()#取绝对值 all([])#可迭代对象中的所有元素都为True 则为True,只要至少一个为Fals ...