一、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. 注册表Demo

    一.获取安装程序信息 #include <windows.h> #include <iostream> #include <string> #include < ...

  2. Linux命令行和shell编程

    Shell Shell是一个程序,用户输入的命令通过shell来传达给内核或其它程序.用户在linux打开一个终端,终端就会自动调用用户的shell. Linux上的Shell有很多种,用的最多是sh ...

  3. Qt的槽可以使用默认参数

    引用自:http://www.ibm.com/developerworks/cn/linux/guitoolkit/qt/signal-slot/ 的一篇经典文章,是关于Qt的信号和槽的分析的.看年份 ...

  4. Codility----OddOccurrencesInArray

    Task description A non-empty zero-indexed array A consisting of N integers is given. The array conta ...

  5. Codility------CyclicRotation

    Task description A zero-indexed array A consisting of N integers is given. Rotation of the array mea ...

  6. java-mysql(1)

    用java写过不少单侧,用到的数据存储也是用xml或者直接文件,但是关于数据库这块很少用到,最近就学习了下java链接mysql数据库. 第一:创建一个测试用的数据库 Welcome to the M ...

  7. orm多表操作

    一.创建表 1.一对多 必须在"多"的表中创建关联字段,在外加约束 class Book(models.Model): id=models.AotuField(primary_ke ...

  8. 在本地安装RabbitMQ Server教程以及可能遇到的问题及解决办法

    1. Download latest erlang OTP platform from : erlang:http://www.erlang.org/download.html (The latest ...

  9. Java基础(六) static五大应用场景

    static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.上一个章节我们讲了final关键字的原理及用法,本 ...

  10. chrome和safari字体粗细问题

    因为我用的是mac电脑,写项目所遇到的问题,这也是我上网和手动试了多次,觉得有效,分享给大家 -webkit-font-smoothing: subpixel-antialiased; -webkit ...