python课堂整理15---map, filter,reduce函数
一、map函数
处理序列(可迭代对象)中的每一个元素,得到的结果是一个‘列表’(其实是个迭代器),该‘列表’元素个数及位置与原来一样
理解下面这段代码:
num_l = [1, 2, 4, 6]
def add_one(x):
return x + 1 #定义一个自加1的函数
def map_test(func, array):
ret = []
for i in array:
res = func(i)
ret.append(res)
return ret print(map_test(add_one, num_l))
print(map_test(lambda x:x + 1, num_l)) #这样更好

用map函数实现
num_l = [1, 2, 4, 6]
print(list(map(lambda x:x + 1,num_l))) #map函数得到的是可迭代对象,需要用list方法处理一下

map函数也可以传入自定义函数
num_l = [1, 2, 4, 6]
def add_one(x):
return x + 1 #定义一个自加1的函数
print(list(map(add_one,num_l)))

用map函数处理字符串
msg = "dabai"
print(list(map(lambda x: x.upper(), msg)))

二、filter函数
filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是Ture则保留下来
理解下面代码:
movie_people = ['sb_123', 'sb_456', 'sb_789', 'dabai']
def filter_test(func, array):
ret = []
for i in movie_people:
if not func(i):
ret.append(i)
return ret
res = filter_test(lambda x: x.startswith('sb'), movie_people) #想把函数运行的结果保存下来就用变量接收,方便下面使用
print(res)

filter内也能自定义函数
movie_people = ['sb_123', 'sb_456', 'sb_789', 'dabai']
def sb_show(x):
return x.startswith('sb')
def filter_test(func, array):
ret = []
for i in array:
if not func(i):
ret.append(i)
return ret
res = filter_test(sb_show, movie_people) #想把函数运行的结果保存下来就用变量接收,方便下面使用
print(res)

用filter函数实现
movie_people = ['sb_123', 'sb_456', 'sb_789', 'dabai']
print(list(filter(lambda x: not x.startswith('sb'), movie_people)))
用filter处理字典
people = [
{'name':'alex','age':10000},
{'name':'dabai','age':18},
{'name':'sb','age':90000}
]
print(list(filter(lambda x: x['age'] <= 18, people)))

三、reduce函数
处理一个序列,然后把序列进行合并操作
理解下面代码
num_l = [1, 2, 3, 100]
def reduce_test(func, array):
res = array.pop(0)
for num in array:
res = func(res, num)
return res print(reduce_test(lambda x, y : x * y,num_l)) #把列表里的值相乘

可以设置一个默认的初始值
num_l = [1, 2, 3, 100]
def reduce_test(func, array, init = None):
if init is None:
res = array.pop(0)
else:
res = init
for num in array:
res = func(res, num)
return res print(reduce_test(lambda x, y : x * y,num_l, 100)) #把列表里的值相乘

使用reduce函数,要先引入functools模块
num_l = [1, 2, 3, 100]
from functools import reduce
print(reduce(lambda x,y : x * y, num_l, 100))

from functools import reduce
print(reduce(lambda x,y : x + y, range(1,101)))

python课堂整理15---map, filter,reduce函数的更多相关文章
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- Python面试题之Python中的lambda map filter reduce zip
当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...
- python map() filter() reduce()函数的用法以及实例
map() 看一下我的终端咋说: map()的函数用法: map(function, iterable, ...) 看一下具体例子: 注意的是一定要强制转化一下才能输出 也可以写匿名函数: (mark ...
- Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)
感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- python Map()和reduce()函数
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
随机推荐
- windows 消息列表
private const UInt32 WM_ACTIVATE = 0x0006; private const UInt32 WM_ACTIVATEAPP = 0x001C; private con ...
- 《C++ Primer》读书笔记 第三章
1.注意:头文件不应包含using声明.因为头文件的内容会拷贝到所有引用他的文件中去,对于某些程序来说,由于不经意间包含了一些名字,可能会产生名字冲突.2.string类型的读入:用cin读入stri ...
- 阿里云CDN+OSS完成图片加速
我们使用React Native开发APP,在列表中显示图片时,列表框经常出现长时间的空白.经过稍微研究和参考其他人的经验,我们知道React Native的ListView以及后来改进版的FlatL ...
- abp(net core)+easyui+efcore仓储系统——领域层创建实体(三)
abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...
- 627.Swap Salary-(LeetCode之Database篇)
问题描述 给出下面的表,名为salary. id name sex salary 1 A m 2500 2 B f 1500 3 C m 5500 4 D f 500 要求执行一个UPDATE语句,将 ...
- 前后端分离时代,Java 程序员的变与不变!
事情的起因是这样的,有个星球的小伙伴向邀请松哥在知乎上回答一个问题,原题是: 前后端分离的时代,Java后台程序员的技术建议? 松哥认真看了下这个问题,感觉对于初次接触前后端分离的小伙伴来说,可能都会 ...
- Python 爬虫从入门到进阶之路(八)
在之前的文章中我们介绍了一下 requests 模块,今天我们再来看一下 Python 爬虫中的正则表达的使用和 re 模块. 实际上爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网 ...
- Go语言之从0到1实现一个简单的Redis连接池
Go语言之从0到1实现一个简单的Redis连接池 前言 最近学习了一些Go语言开发相关内容,但是苦于手头没有可以练手的项目,学的时候理解不清楚,学过容易忘. 结合之前组内分享时学到的Redis相关知识 ...
- 【设计模式】结构型04桥接模式(Bridge Pattern)
学习地址:http://www.runoob.com/design-pattern/bridge-pattern.html 桥接模式(Bridge Pattern) 桥接模式(Bridge patte ...
- laravel配置不同环境的配置文件
//在入口bootstrap/App.php中 $env = $app->detectEnvironment( function () use ($app) { $uname = php_una ...