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 ...
随机推荐
- asp.net core系列 66 Dapper介绍--Micro-ORM
一.概述 目前对于.net的数据访问ORM工具很多,EF和EF Core是一个重量级的框架.最近在搭建新的项目架构,来学习一下轻量级的数据访问ORM工具Dapper.Dapper支持SQL Serve ...
- 【Java源码】集合类-JDK1.8 哈希表-红黑树-HashMap总结
JDK 1.8 HashMap是数组+链表+红黑树实现的,在阅读HashMap的源码之前先来回顾一下大学课本数据结构中的哈希表和红黑树. 什么是哈希表? 在存储结构中,关键值key通过一种关系f和唯一 ...
- 从理论到实践,全方位认识HTTP/2
前言 为了降低加载时间,相信大多数人都做过如下尝试 - Keep-alive: TCP持久连接,增加了TCP连接的复用性,但只有当上一个请求/响应完全 完成后,client才能发送下一个请求 ...
- docker无法启动
docker无法启动 docker启动时报错Error starting daemon: SELinux is not supported with the overlay2 graph driver ...
- 另一个ACM之路建议
ACM联系建议 一位高手对我的建议: 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划 ...
- POJ 3680:Intervals(最小费用最大流)***
http://poj.org/problem?id=3680 题意:给出n个区间[Li,Ri],每个区间有一个权值wi,要使得每个点都不被超过k个区间覆盖(最多能被k个区间覆盖),如果选取了第i个区间 ...
- redis 命令的调用过程
参考文献: Redis 是如何处理命令的(客户端) 我是如何通过添加一条命令学习redis源码的 从零开始写redis客户端(deerlet-redis-client)之路--第一个纠结很久的问题,r ...
- js random获取随机数,获取任意范围内随机整数
壹 ❀ 引 想着好久没做笔试题了,去GitHub找了面试相关的项目,结果被第一道题难住了.....说难其实也不难,而是我忘记了取范围随机整数怎么写了,不可否认如果当时是我在笔试,肯定也凉了,那么就由 ...
- C# 管道式编程
受 F# 中的管道运算符和 C# 中的 LINQ 语法,管道式编程为 C# 提供了更加灵活性的功能性编程.通过使用 扩展函数 可以将多个功能连接起来构建成一个管道. 前言 在 C# 编程中,管道式编程 ...
- python之pip install
安装方式1 wget http://python-distribute.org/distribute_setup.py sudo python distribute_setup.py wget h ...