python 函数map()、filter()、reduce()
map()函数 将一个列表进行遍历,对每一个字符串进行处理:
例如:
num_list = ["我","是","哈哈","太平洋海工欢唱","六队船厂","六队码头","六队船坞"]
def add_function(x):
return x + 1
def reduce(x):
return x - 1
def binary(x):
return x ** 2
def test(func, x):
new_list = []
for i in x:
new_list.append(func(i))
return new_list
print(list(map(lambda x:str(x)+"叠加",num_list)))
输出
['我叠加', '是叠加', '哈哈叠加', '太平洋海工欢唱叠加', '六队船厂叠加', '六队码头叠加', '六队船坞叠加']
进行对可迭代的对象进行单个处理。
filter()函数 ,对可遍历的对象进行过滤。适合进行字符串处理。
例如:
num_list = ["我","是","哈哈","太平洋海工欢唱","六队船厂","六队码头","六队船坞"]
def filter_list(func,array):
res = []
for i in array:
if func(i):
res.append(i)
return res
print(list(filter_list(lambda x:x.startswith("六队"),num_list)))
print(list(filter(lambda x:x.startswith("六队"),num_list)))
输出
['六队船厂', '六队码头', '六队船坞']
['六队船厂', '六队码头', '六队船坞']
该函数可以对字符串进行处理。返回一个列表的对象。
reduce() 函数, 该函数对整数进行处理。加减乘除都可以。
例如:
from functools import reduce
a = [1, 2, 3, 100]
lambda x, y: x * y def num(array, func,init = None):
if init == None:
res = array.pop(0)
else:
res = init
for i in array:
res = func(res, i)
return res
print(reduce(lambda x,y:x*y,a,100))
print(num(a, lambda x, y: x * y,100))
输出
60000
60000
该函数适合对,某个可迭代的对象进行数据的运算。
python 函数map()、filter()、reduce()的更多相关文章
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- 高阶函数-map/filter/reduce
什么样的函数叫高阶函数: 条件:1.函数接受函数作为参数 2.函数的返回值中包含函数 高阶函数之----map函数 map(func, *iterables) --> map objectnum ...
- python几个特别函数map filter reduce lambda
lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x): return x**2 print f(4) Python中使用lambda的话,写成这样 g = l ...
- python之内置函数:map ,filter ,reduce总结
map函数: #处理序列中的每个元素,得到的结果是一个'列表',该列表元素个数及位置与原来一样 filter函数: #遍历序列中的每个元素,判断每个元素得到一个布尔值,如果是true,则留下来 peo ...
- 高阶函数map(),filter(),reduce()
接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...
- 高阶函数map,filter,reduce的用法
1.filter filter函数的主要用途是对数组元素进行过滤,并返回一个符合条件的元素的数组 let nums = [10,20,30,111,222,333] 选出nums中小于100的数: 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, ...]) -& ...
随机推荐
- Hibernate面试题(七)--load与get区别
1. load方式是延迟加载,只有属性被访问的时候才会调用sql语句 get方式是非延迟加载,无论后面的代码是否会访问到属性,马上执行sql语句 2. 都通过id=500去获取对象1. get方式会返 ...
- SpringMVC 入门demo
(1)新建Spring项目 (2)添加所需要的jar包 spring的5+2: spring-core.jar spring.beans.jar spring-context.jar spring-e ...
- 类似discuz密码的生成规则
/* 生成一个串,uniqid(rand()): uniqid(prefix,more_entropy) 函数基于以微秒计的当前时间,生成一个唯一的 ID. 如果 prefix 参数为空,则返回的字符 ...
- Python3.5学习之旅——day1
本节内容: 1.Python介绍 2.Hello World程序 3.变量\字符编码 4.用户输入 5.if-else语句 6.循环语句 一.Python介绍 Python是一种动态解释性的强类型定义 ...
- 操作系统OS - 重装Windows7卡在completing installation
1. shift + f10 2. cd oobe 3. Msoobe
- MyBatis+Oracle实现主键自增长的几种常用方式
一.使用selectKey标签 <insert id="addLoginLog" parameterType="map" > <selectK ...
- linux目录与路径
1.相对路径和绝对路径 绝对路径:一定是从根目录开始,如:/usr/share/doc 相对路径:如果想从/usr/share/doc/到/usr/share/man下,可以写成 cd ../man, ...
- Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)
There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health ...
- Python学习笔记002
字符编码:把二进制字符翻译成字符 ASCII码表 256 一个字节,8个比特 支持中文: GB2312 GBK1.0 GB18030 BIG5(台湾) unicode UTF-8 开头定义 ...
- Linux命令:yum命令
YUM: Yellowdog Update Modifier,rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具 一.yum命令用法 yum repolist ...