一、map、filter、reduce

  map(fuction , iterable)   映射  对可迭代对象中的每一项,使用函数去改变

  filter(function, iterable)  过滤  可迭代对象中的每一项,放到函数中去计算,如何为真,则留下,构造成一个迭代器,为假则去除

  reduce(fuction,iterable)  减少  把元素中的左边的合并到右边去。

1.1 map  映射

  官方定义:Return an iterator that applies function to every item of iterable, yielding the results.

        返回一个迭代器,它对每个迭代项应用函数,得到结果。

  why:不使用for循环,就能将序列中的数据m--映射到给定的处理函数中。快速的对一个序列进行各种操作

numbers = [1, 3, 5, 7, 9]

# 改写成2,4,6,8,10

#普通做法
new_num = []
for x in numbers:
new_num.append(x+1) print(new_num) # map版本
def addone(x):
return x+1 print(list(map(addone, numbers))) # 其他应用 改写字符串
str_list = ['lilei', 'hmm', 'de8ug']
def change(s: str):
return s.upper() print(list(map(change, str_list)))

1.2 filter 过滤、筛选

  官方: Construct an iterator from those elements of iterable for which function returns true.

      iterable may be either a sequence, a container which supports iteration, or an iterator.

      If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

      从可迭代的元素中构造一个迭代器,函数返回true。iterable可以是一个序列,一个支持迭代的容器,

      或者一个迭代器。如果函数为None,则假定标识函数为false,即为 false的所有元素都被删除。

  why:不用for循环, 就能将序列中的数据一一映射到给定 的处理函数, 函数中添加了真假判断,

     True则返回 相应数据,最终得到筛选后的序列。

'''
查找大于30 小于50的数字列表
'''
my_list = [24,23,75,12,43,9,42,28,37]
# 普通版本
new_list = [] for i in my_list:
if 30 < i < 50:
new_list.append(i)
print(new_list) # filter版本
def choose(x):
# if 30 < i < 50:
# return True
return 30 < x < 50 # < 操作的结果,已经是bool 类型了 print(list(filter(choose, my_list))) # 字符串操作
import re
str_list = ['lilei', 'hmm', 'de8ug', 'debug1', 'de8ug2'] def lh(s: str):
return re.search('de8ug', s) print(list(filter(lh, str_list)))

1.3 reduce 减少,合并

  官方:Apply function of two arguments cumulatively to the items of sequence, from left to right,

     so as to reduce the sequence to a single value.

     将两个参数的函数累积到序列的项上,从左到右,以便将序列减少到单个值。

  why: 为了快速的进行累加,连乘的计算 使代码更简洁

  需要注意:py3中吧reduce放到了fuctiontools这个模块下了

from functools import reduce

numbers = [1, 3, 5, 7, 9]

# 普通版本 累加
count = 0
for i in numbers:
count += i
print(count) # reduce版 累加
def add(x, y):
return x+y print(reduce(add, numbers)) # 普通版本 连乘
count = 1
for i in numbers:
count *= i
print(count) # reduce版本 连乘
def mul(x, y):
return x*y
print(reduce(mul, numbers))

二、 lambda表达式

2.1  基本知识概览

2.2 代码部分"1. 单参数 和 判断"

f = lambda x: x*2
f(2) # f = lambda x: True if x>8 else False
print(f(9)) # True "2. 多参数"
x = lambda x,y: x*y+2
print(x(2,3)) # "3. 排序"
users = [('de8ug', 18, 1), ('lilei', 20, 2), ('hmm', 17, 3), ('zhangsan', 30, 4)]
users.sort(key=lambda x: x[1]) # x代表字典,每一个数组,要对谁排序,直接加上该位置
users.sort(key=lambda x:(x[1],x[2])) . # 双排序
print(users) "4. 字典的默认值"
from collections import defaultdict # 生成默认的数值
d = defaultdict(lambda :0)
d[1]
print(d) # defaultdict(<function <lambda> at 0x101ab9950>, {1: 0}) # 生成默认的字符串
d = defaultdict(lambda :'abc')
d[1]
print(d) # defaultdict(<function <lambda> at 0x101ab9950>, {1: 'abc'}) # 生成二元数组,给地理信息使用
point = defaultdict(lambda :(2,3))
point['p1']
print(point) # defaultdict(<function <lambda> at 0x105a89950>, {'p1': (2, 3)}) "5. 在map,reduce,filter中使用,可以减少函数的定义"
# 代码省略,看上面部分的代码

  

map、filter、reduce、lambda的更多相关文章

  1. ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)

    目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...

  2. python的map,filter,reduce学习

    python2,python3中map,filter,reduce区别: 1,在python2 中,map,filter,reduce函数是直接输出结果. 2,在python3中做了些修改,输出前需要 ...

  3. Python 函数之lambda、map、filter和reduce

    1.lambda函数 lambda()是Python里的匿名函数,其语法如下: lambda [arg1[, arg2, ... argN]]: expression 学习条件运算时,对于简单的 if ...

  4. python迭代和解析(3):range、map、zip、filter和reduce函数

    解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html range range()是一个内置函数,它返回一个数字序列,功能和Li ...

  5. python高阶函数,map,filter,reduce,ord,以及lambda表达式

    为什么我突然扯出这么几个函数,是因为我今天在看流畅的python这本书的时候,里面有一部分内容看的有点懵逼. >>> symbols = '$¢£¥€¤' >>> ...

  6. 内置函数--map,filter,reduce

    一.map class map(object): """ map(func, *iterables) --> map object Make an iterator ...

  7. python的 map,filter, reduce, enumerate

    一, map     #基本的map运用都可以用解析去替代,复杂的仍然需要定义函数,利用map去做 map(函数, 序列) 将序列的各项经过函数处理, 然后返回到一个新列表中. #itertools. ...

  8. Map、Filter和Reduce函数(Python)

    Map map(function_to_apply, list_of_inputs) 设有以下代码: >>> items = [1, 2, 3, 4, 5] >>> ...

  9. 160909、Filter多方式拦截、禁用IE图片缓存、Filter设置字符编码

    dispatcher多方式拦截 我们来看一个例子 我们定义一个index.jsp,里面有一个链接跳转到dispatcher.jsp页面 <body> <a href="di ...

  10. Servlet生命周期 、Filter生命周期、Listering(监听器)总结

    Servlet生命周期简述 (1)加载和实例化 当Servlet容器启动或客户端发送一个请求时,Servlet容器会查找内存中是否存在该Servlet实例,若存在,则直接读取该实例响应请求:如果不存在 ...

随机推荐

  1. XDU 1022 (数论筛法+前缀和)

    解法一:数论筛法+前缀和 //其实题目中f[n]的值可理解为存在多少个整数对使a*b<=n #include<cstdio> #define N 1007 #define maxn ...

  2. 在Idea中连接数据库并生成实体类(mybatis逆向生成实体类)

    1.连接数据库 (1)按下图 ,  点击view-----选择tool windows----------选择database并点击 (2)弹出Database窗口 点击加号------------选 ...

  3. es6函数模块-------初步学习

    初步学习: 函数参数允许尾逗号 function clownsEverywhere( param1, param2, //param2后面有逗号 ) { } 函数参数可以赋初值 利用解构赋值默认值结合 ...

  4. web前端基础——初识HTML

    1 HTML概念 HTML(Hypertext Markup Language)即超文本标记语言,是网页的描述语言.它其实是一种描述网页的标准,它通过给需要描述的内容加上标签,浏览器按照HTML语言的 ...

  5. 如何做好Web接口测试

    说说我在测试接口时遇到的一些需要注意的点: 1.接口返回:数据格式是否与预期一致.例如:要求返回json格式的数据,json数据的key命名是否正确,对应的value是否与数据库一致.需要转换的数据是 ...

  6. java并发再次积累

    监视器与锁之间的关系: 1.object monitor 是任何一个对象都有的内置的数据结构,它是用来协调使用当前对象的多个线程之间的执行顺序的(wait/notify),线程会block或者wait ...

  7. AVAudioSession(1):iOS Audio Session 概览

    本文转自:AVAudioSession(1):iOS Audio Session 概览 | www.samirchen.com 本文内容主要来源于 Audio Session Programming ...

  8. Spring事务回滚

    配置事物: @Configuration /**强制使用cglib代理时就把proxy-target-class设为true.*/ @EnableTransactionManagement(proxy ...

  9. Ubuntu 18.04配置机场客户端

    最近把自己的笔记本电脑安装成ubuntu18.04操作系统,为了更方便的查找文档,所以需要配置一下机场(v2ray)的客户端方便查找资料,以下是配置步骤: 1.下载并执行一键脚本: bash < ...

  10. mysql增量备份脚本

    #!/bin/sh ############################### # 此脚本用来增量备份 # 此文件名:mysqldailybackup.sh # # Author: zhangro ...