一、map函数

1、自定义函数,实现类似于map函数的功能

 num_l = [1,3,4,5,6,9]
def power(n):
return n ** 2
def map_test(func,array):
li0 = []
for i in array:
p = func(i)
li0.append(p)
return li0 f = map_test(power,num_l) 运用自己定义的函数来计算
print(f)
f = map_test(lambda x: x ** 2, num_l) #调用匿名函数实现简单的功能,减少代码量,以下几种类似
print(f) def add_one(n):
return n + 1 f1 = map_test(add_one,num_l)
print(f1)
f1 = map_test(lambda x: x+1, num_l)
print(f1) def reduce_one(n):
return n - 1 f2 = map_test(reduce_one,num_l)
print(f2)
f2 = map_test(lambda x: x - 1, num_l)
print(f2)
 [1, 9, 16, 25, 36, 81]
[1, 9, 16, 25, 36, 81]
[2, 4, 5, 6, 7, 10]
[2, 4, 5, 6, 7, 10]
[0, 2, 3, 4, 5, 8]
[0, 2, 3, 4, 5, 8]

2、map函数的运用:作用于成哥序列,让整个序列实现想要的转换

 ############n内置函数 map 的使用
num_l = [1,3,4,5,6,9]
f3 = map(lambda x:x + 3, num_l) # map(func, *iterables) --> map object 这是map函数官方解释
print(f3)
print(list(f3)) #注意细节:map返回只是一个object,需要用list形式打印出来 s = 'abcefg'
f4 = map(lambda st:st.upper(),s)
print(f4)
print(list(f4))
 <map object at 0x000001C6AC2B7860>
[4, 6, 7, 8, 9, 12]
<map object at 0x000001C6AC2B7898>
['A', 'B', 'C', 'E', 'F', 'G']

二、filter函数

1、自定义函数,实现类似于filter的功能

例1:铺垫

 bjper = ['bj_老王','bj_老赵','bj_老李','tian an men','gugong']
def filter_test(array):
li0 = []
li1 = []
for i in array:
if i.startswith('bj'):
li0.append(i)
if not i.startswith('bj'):
li1.append(i)
return li0,li1 f = filter_test(bjper)
print(f)
 (['bj_老王', 'bj_老赵', 'bj_老李'], ['tian an men', 'gugong'])

例2

 def show_bj(s):
return s.startswith('bj') bjper = ['bj_老王ha','bj_老赵','bj_老李','tian an menha','gugongha']
def filter_test(func,array):
li0 = []
for i in array:
if func(i):
li0.append(i)
return li0 f = filter_test(show_bj,bjper)
print(f)
# lambad 运用
f = filter_test(lambda s:s.endswith('ha'),bjper)
print(f)
 ['bj_老王ha', 'bj_老赵', 'bj_老李']
['bj_老王ha', 'tian an menha', 'gugongha']

2、filter函数运用:主要筛选出想要的元素

 ################ filter 应用:官方解释:filter(function or None, iterable) --> filter object
bjper = ['bj_老王ha','bj_老赵','bj_老李','tian an menha','gugongha']
def show_bj(s):
return s.startswith('bj')
f1 = filter(show_bj,bjper)
print(f1)
print(list(f1)) #注意细节:filter返回只是一个object,需要用list形式打印出来 f2 = filter(lambda st: not st.endswith('ha'),bjper)
print(f2)
print(list(f2))
 <filter object at 0x00000218E63A7898>
['bj_老王ha', 'bj_老赵', 'bj_老李']
<filter object at 0x00000218E63A78D0>
['bj_老赵', 'bj_老李']

三、reduce函数:

1、

例1

 num_l = [2,4,10,100]
init = 0
for i in num_l:
init += i
print(init)

结果:116

例2

 num_l = [2,4,10,100]
def sum_test(array):
init = 0
for i in array:
init += i
return init
f = sum_test(num_l)
print(f)

结果:116

例3

 num_l = [2,4,10,100]
init = 1
for i in num_l:
init *= i
print(init)

结果8000

例4

 num_l = [2,4,10,100]
def mul(array):
init = 1
for i in array:
init *= i
return init f = mul(num_l)
print(f)

结果:8000

例5

 num_l = [2,4,10,100]
def reduce_test(func,array,init=None):
init = array.pop(0)
for i in array:
init = func(init,i)
return init def product(x,y):
return x * y f = reduce_test(product,num_l,)
print(f)

结果:8000

例6

 num_l = [2,4,10,100]
def reduce_test(func,array,init=None):
if init == None:
res = array.pop(0)
else:
res = init
for i in array:
res = func(res,i)
return res def product(x,y):
return x * y f = reduce_test(product,num_l,4)
print(f)

结果:32000

例7

 ############# reduce 函数
#使用前需要导入reduce函数包
from functools import reduce num_l = [2,4,10,100]
def product(x,y):
return x * y f1 = reduce(product,num_l,5)
print(f1) f1 = reduce(lambda x,y: x + y + 1,num_l,1000)
print(f1)
 40000
1120

十一、python沉淀之路--map函数、filter函数、reduce函数、匿名函数、内置函数的更多相关文章

  1. Python成长之路第二篇(2)_列表元组内置函数用法

    列表元组内置函数用法list 元组的用法和列表相似就不一一介绍了 1)def append(self, p_object):将值添加到列表的最后 # real signature unknown; r ...

  2. python基础之递归,匿名,内置函数

    递归函数: 什么是递归函数? 函数递归调用:在调用一个函数的过程中,又直接或间接地调用了该函数本身. 递归必须要有两个明确的阶段: ①递推:一层一层递归调用下去,强调:每进入下一层问题规模减少 ②回溯 ...

  3. 函数进阶· 第3篇《常用内置函数filter()、map()、zip(),怎么用的呢?》

    坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:oschina.云+社区.知乎等各大平台都有. 由于微信公众号推送改为了信息流的形式,防止走丢,请给加个星标 ,你就可以第一时间接收到本公众号的推送! ...

  4. python基础12_匿名_内置函数

    一个二分查找的示例: # 二分查找 示例 data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35, 36, ...

  5. Python全栈开发之3、深浅拷贝、变量和函数、递归、函数式编程、内置函数

    一.深浅拷贝 1.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy # 定义变量 数字.字符串 # n1 = 123 n1 ...

  6. Python——day14 三目运算、推导式、递归、匿名、内置函数

    一.三目(元)运算符 定义:就是 if...else...语法糖前提:简化if...else...结构,且两个分支有且只有一条语句注:三元运算符的结果不一定要与条件直接性关系​ cmd = input ...

  7. python之三元表达式与生成式与匿名与内置函数(部分)

    目录 三元表达式 各种生成式 列表生成式(可同样作用于集合) 字典生成式 匿名函数 重要内置函数 map() zip() filter() reduce() 常见内置函数(部分) 三元表达式 三元表达 ...

  8. Python函数的基本定义和调用以及内置函数

    首先我们要了解Python函数的基本定义: 函数是什么? 函数是可以实现一些特定功能的小方法或是小程序.在Python中有很多内建函数,当然随着学习的深入,你也可以学会创建对自己有用的函数.简单的理解 ...

  9. Python文件操作函数os.open、io.open、内置函数open之间的关系

    Python提供了多种文件操作方式,这里简单介绍os.open.io.open.内置函数open之间的关系: 一.内置函数open和io.open实际上是同一个函数,后者是前者的别名: 二.os.op ...

随机推荐

  1. nginx 第一天

    来公司第一天先装了一下nginx 第一步:  先装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/ ...

  2. NOIP 转圈游戏

    描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推 ...

  3. Routing and Action Selection in ASP.NET Web API

    https://exceptionnotfound.net/using-http-methods-correctly-in-asp-net-web-api/ The algorithm ASP.NET ...

  4. [POI2006]MET-Subway

    Description 给出一棵N个结点的树,选择L条路径,覆盖这些路径上的结点,使得被覆盖到的结点数最多. Input 第一行两个正整数N.L(2 <= N <= 1,000,000, ...

  5. [BZOJ2717]迷路的兔子[构造]

    构造题…当然需要推(看)一推(看)规(题)律(解)啦... 其实是在Discuss那个CA的一句话题解里面翻到这个东西的... 用奇怪的姿势枚举一下...先贴代码 #include<bits/s ...

  6. PAT1054. The Dominant Color (20)

    #include <iostream> #include <map> using namespace std; int n,m; map<int,int> imgM ...

  7. Linux新手常用命令 - 转载

    开始→运行→cmd命令 集锦 cls------------命令窗清屏eqit-----------退出当前命令ping ip--------检查网络故障ipconfig-------查看IP地址wi ...

  8. Memcached replace 命令

    Memcached replace 命令用于替换已存在的 key(键) 的 value(数据值). 如果 key 不存在,则替换失败,并且您将获得响应 NOT_STORED. 语法: replace ...

  9. css滚动相关问题记录

    1) 关于滑动加速优化,可以通过css进行处理 例如,html如下: <div class="content-dialog"> <h1>活动规则</h ...

  10. 本地动态SQL

    (转自:http://blog.itpub.net/26622598/viewspace-718134) 一.什么是动态SQL 大多数PL/SQL都做着一件特殊的结果可预知的工作.例如,一个存储过程可 ...