一、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函数的更多相关文章

  1. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

  2. Python面试题之Python中的lambda map filter reduce zip

    当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...

  3. python map() filter() reduce()函数的用法以及实例

    map() 看一下我的终端咋说: map()的函数用法: map(function, iterable, ...) 看一下具体例子: 注意的是一定要强制转化一下才能输出 也可以写匿名函数: (mark ...

  4. Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...

  5. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  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 ...

  7. python Map()和reduce()函数

    Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...

  8. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  9. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  10. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

随机推荐

  1. hadoop之hive建表语句备份

    转自:https://blog.csdn.net/t___z/article/details/78492113 #!/bin/bash hive -e "use lbi;show table ...

  2. Python连载12-shutil模块

    一.shutil模块 1.函数:copy() (1)用法:复制文件0 (2)格式:copy(来源路径,目标路径) (3)返回值:返回目标路径 (4)注意:拷贝的同时可以给文件重命名 source_pa ...

  3. redis连接错误3种解决方案System Error MISCONF Redis is configured to save RDB snapshots

    redis连接错误System Error MISCONF Redis is configured to save RDB snapshots, but XX   情况1解决办法: 由于强制停止red ...

  4. Metasploit学习笔记

    原创博客,转载请注出处! 各位看官可参看——Metasploit实验操作 1.打开msf        msfconsole2.帮助选项:    msfconsole -h        显示在msf ...

  5. [Vue 牛刀小试]:第十四章 - 编程式导航与实现组件与 Vue Router 之间的解耦

    一.前言 在上一章的学习中,通过举例说明,我们了解了 Vue Router 中命名路由.命名视图的使用方法,以及如何通过 query 查询参数传参,或者是采用 param 传参的方式实现路由间的参数传 ...

  6. HTML行内元素、块级元素、行内块级元素的特点与区别

    元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 元素分类方式 HTML 可以将元素分类方式分为行内元素.块状元素和行内块状元素三种,这三者可以通过设 ...

  7. 【Dubbo】Dubbo+ZK基础入门以及简单demo

    参考文档: 官方文档:http://dubbo.io/ duboo中文:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/backgr ...

  8. paxos算法——今生

    Paxos 定义2.1  票:即弱化形式的锁.它具备下面几个性质: 可重新发布:服务器可以重新发布新票,即使前面发布的票没有释放. 票可以过期:客户端用一张票来给服务器发送命令请求时,只有当这张票是最 ...

  9. ELK架构下利用Kafka Group实现Logstash的高可用

    系统运维的过程中,每一个细节都值得我们关注 下图为我们的基本日志处理架构 所有日志由Rsyslog或者Filebeat收集,然后传输给Kafka,Logstash作为Consumer消费Kafka里边 ...

  10. python之内置函数,匿名函数,递归函数

    一. 内置函函数 什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就 ...