map

num_l = [1,6,8,9]
def map_test(func,array):
ret = []
for i in array:
res = func(i)
ret.append(res)
return ret
def jia(x):
return x+1 #内置函数
print(map_test(lambda x:x+1,num_l)) print(map_test(jia,num_l)) #只能迭代一次
# res = map(lambda x:x+1,num_l)
# for i in res:
# print(i)
# print(list(res))

filter

#filter函数
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
print(filter(lambda n:not n.endswith('sb'),movie_people)) res=filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res)) print(list(filter(lambda n:not n.endswith('sb'),movie_people)))

reduce

# 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))
#处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样
# map() #filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来 people=[
{'name':'alex','age':1000},
{'name':'wupei','age':10000},
{'name':'yuanhao','age':9000},
{'name':'linhaifeng','age':18},
]
print(list(filter(lambda p:p['age']<=18,people))) #reduce:处理一个序列,然后把序列进行合并操作
from functools import reduce
print(reduce(lambda x,y:x+y,range(100),100))
print(reduce(lambda x,y:x+y,range(1,101)))

16python的map函数,filter函数,reduce函数的更多相关文章

  1. Python的map、filter、reduce函数 [转]

    1. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq): mapped_seq = []        fo ...

  2. python中的map、filter、reduce函数

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...

  3. python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数

    函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...

  4. map、filter、reduce函数的使用

    1.filter() 作用:过滤 // 1.筛选出大于30的数. const array = [10, 20, 30, 40, 50, 60, 70, 80] // 普通写法 // let newar ...

  5. python学习-day15:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r

    ---恢复内容开始--- 一.全局变量与局部变量 在子程序中定义的变量称为局部变量, 在程序的一开始定义的变量称为全局变量. 全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序.当全局变量与 ...

  6. python学习-day16:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r

    一.作用域 作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变 二.匿名函数 lambda:正常和其他函数进行配合使用.正常无需把匿名函数赋值给一个变量. f=lambda x:x*x p ...

  7. map、filter、reduce函数

    map #函数需要⼀个参数 m1 = map(lambda x:x*x,[1,2,3]) print(list(m1)) #函数需要两个参数 m2 = map(lambda x,y:x+y,[1,2, ...

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

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

  9. python的map,filter,reduce学习

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

  10. Swift函数编程之Map、Filter、Reduce

    在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...

随机推荐

  1. 【NS2】NS2修改MAC协议(转载)

    NS2版本:2.34   涉及NS2代码文件: ns-2.34/mac/channel.h ns-2.34/mac/channel.cc ns-2.34/mac/wireless-phyExt.h n ...

  2. 逆序对(POJ2299 Ultra-QuickSort)

    #include<bits/stdc++.h> using namespace std; int n; ],b[],ans;//a为待排序数组,b为临时数组,ans为逆序对数 void m ...

  3. 30 Cool Open Source Software I Discovered in 2013

    30 Cool Open Source Software I Discovered in 2013 #1 Replicant – Fully free Android distribution Rep ...

  4. H3C SSH配置例子

  5. 【微信小程序】下载并预览文档——pdf、word、excel等多种类型

    .wxml文件 <view data-url="https://XXX/upload/zang." data-type="excel" catchtap= ...

  6. java的System.currentTimeMillis()如何转换成C#的DateTime.Now.Ticks?

    考虑到我们是东八时区的话,应做如下转换: long milli = System.currentTimeMillis() + 8*3600*1000; long ticks = (milli*1000 ...

  7. rsa加密(非对称加密)

    rsa加密 是非对称加密 需要公钥 与 私钥 这个公钥私钥的具体值需要与后端协商定下 rsa js代码如下 代码太多不插入了 html代码如下 <!DOCTYPE html> <ht ...

  8. Codeforces Round #172 (Div. 1 + Div. 2)

    A. Word Capitalization 模拟. B. Nearest Fraction 枚举. C. Rectangle Puzzle 求出两个矩形的点,套简单多边形的面积交板子. D. Max ...

  9. js实现点击隐藏图片

    方法一: 把图片的display设为none,触发点击事件时,display变为block <style> img { width: 400px;height: 300px; displa ...

  10. Javassist指引(一)

    目录 原文链接 1. 读写字节码 1.1概述 Javassist是一个Java字节码类库.Java的字节码是包含Java类与接口,并按照一定的顺序存在class文件中. Javassist.CtCla ...