py-day3-6 python map函数
map函数 :处理序列中的每个元素,得到的结果是一个列表,该列表元素个数及位置与原来一样 ## 求列表里元素的平方 (原始方法)
num_1=[1,2,13,5,8,9]
res =[]
for i in num_1:
res.append(i**2)
print('打印结果:',res) 打印结果: [1, 4, 169, 25, 64, 81] 有多个列表求里面元素的平方 (定义成函数)
num_1=[1,2,13,5,8,9]
def test(array):
res =[]
for i in num_1:
res.append(i**2)
return res
end1 = test(num_1)
end2 = test(num_1) # 以后用直接调用函数就可以了
print('打印结果:',end1)
print('打印结果:',end2) 打印结果: [1, 4, 169, 25, 64, 81]
打印结果: [1, 4, 169, 25, 64, 81] 提的需求就是功能,功能就要封装在函数里
num_1=[1,2,13,5,8,9]
def reduce_1(x): # 定义自减1函数
return x-1
def add_1(x): # 定义自增1函数
return x+1
def test(func,array):
res =[]
for i in num_1:
ret=func(i)
res.append(ret)
return res
print('自增1的结果:',test(add_1,num_1))
print('自减1的结果:',test(reduce_1,num_1)) 自增1的结果: [2, 3, 14, 6, 9, 10]
自减1的结果: [0, 1, 12, 4, 7, 8] # 终极版本 最简单的.使用匿名函数同样可以做到
num_1=[1,2,13,5,8,9]
def test(func,array):
res =[]
for i in num_1:
ret=func(i)
res.append(ret)
return res
print('自增1的结果:',test(lambda x:x+1,num_1))
print('自减1的结果:',test(lambda x:x-1,num_1))
print('平方的结果:',test(lambda x:x**2,num_1))
print('除2的结果:',test(lambda x:x/2,num_1)) 自增1的结果: [2, 3, 14, 6, 9, 10]
自减1的结果: [0, 1, 12, 4, 7, 8]
平方的结果: [1, 4, 169, 25, 64, 81]
除2的结果: [0.5, 1.0, 6.5, 2.5, 4.0, 4.5] 内置函数map函数
num_1=[1,2,13,5,8,9]
def test(func,array):
res =[]
for i in num_1:
ret=func(i)
res.append(ret)
return res
print('匿名函数的处理结果:',test(lambda x:x+1,num_1)) ret = map(lambda x:x+1,num_1)
print('内置函数map的处理结果:',list(ret)) 匿名函数的处理结果: [2, 3, 14, 6, 9, 10]
内置函数map的处理结果: [2, 3, 14, 6, 9, 10]
map函数 还适用与自己设定的函数
num_1=[1,2,13,5,8,9]
def reduce_1(x): # 定义自减1函数
return x-1
def add_1(x): # 定义自增1函数
return x+1
def test(func,array):
res =[]
for i in num_1:
ret=func(i)
res.append(ret)
return res
ret = map(reduce_1,num_1)
ret1 = map(add_1,num_1)
print('map处理自减1的结果:',list(ret))
print('map处理自加1的结果:',list(ret1)) map处理自减1的结果: [0, 1, 12, 4, 7, 8]
map处理自加1的结果: [2, 3, 14, 6, 9, 10]
map还以可以迭代其他对象,
msg ='majunnihao' #(例如小写转换成大写)
t = map(lambda x:x.upper(),msg)
print(list(t)) ['M', 'A', 'J', 'U', 'N', 'N', 'I', 'H', 'A', 'O']
py-day3-6 python map函数的更多相关文章
- python map函数(23)
截至到目前为止,其实我们已经接触了不少的python内置函数,而map函数也是其中之一,map函数是根据指定函数对指定序列做映射,在开发中使用map函数也是有效提高程序运行效率的办法之一. 一.语法定 ...
- python map函数
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...
- python map函数 reduce函数
Python中map()函数浅析 函数式编程: 更好的描述问题 map函数 怎么理解当传入多个参数list时,map如何运作: abc函数第一次传入的数据时 (11,44,77),然后(22,5 ...
- python——map()函数
描述 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. 语法 m ...
- python map函数的使用
python2 中的map函数返回列表 python3 中的map函数返回迭代器 >>>def square(x) : # 计算平方数 ... return x ** 2 ... & ...
- python map函数、filter函数、reduce函数
1.map函数:map(func,可迭代对象): ①func可以是自定义的函数,也可以是功能简单的匿名函数(通过lambda定义) ②处理逻辑:表示将传入的可迭代对象依次循环,将每个元素按照传入的fu ...
- day16 Python map函数
num_l=[1,2,10,5,3,7] #lambda x:x+1 # def add_one(x): # return x+1 #lambda x:x+1 # def reduce_one(x): ...
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- python filter函数(40)
一.filter函数简介 filter函数主要用来筛选数据,过滤掉不符合条件的元素,并返回一个迭代器对象,如果要转换为列表list或者元祖tuple,可以使用内置函数list() 或者内置函数tupl ...
随机推荐
- c++中sort()函数的用法
用法: 1.sort函数可以三个参数也可以两个参数,必须的头文件#include < algorithm>和using namespace std; 2.它使用的排序方法是类似于快排的方法 ...
- 移动端设备中1px适配
方式1:伪类+transform实现,主要用transform中的scale缩放,缩放默认中心点是以x,y轴的50%处,因此需要用transform-origin调整中心点 html代码: <d ...
- 让虚拟环境解决python多版本并行
一.安装篇 1.本文操作系统为CentOS7 依赖包(安装时可能还存在其他依赖包,结合报错进行安装) [root@Corre home]# yum install make build-essenti ...
- EOJ Monthly 2019.2 题解(B、D、F)
EOJ Monthly 2019.2 题解(B.D.F) 官方题解:https://acm.ecnu.edu.cn/blog/entry/320/ B. 解题 单测试点时限: 2.0 秒 内存限制: ...
- python基础一之课后作业:编写登录接口
1 # Author : Mamba 2 3 #python基础一之课后作业:编写登录接口 4 5 # 输入用户名密码 6 # 认证成功后显示欢迎信息 7 # 用户名3次输入错误后,退出程序 8 # ...
- nw 引用 sqlite
0.好吧,这对于我这个c 小白来说,真的有点难度. 1.安装Python 2.7.14 https://www.python.org/downloads/ 2.安装最新的nodejs+npm http ...
- vue+koa实现简单的图书小程序(3)
实现一个今年过了多少天的组件的记录我们使用了原生的微信小程序开发文档里的组件 “Progress” 并不需要自己去写: https://developers.weixin.qq.com/minipro ...
- Beta冲刺第二周王者荣耀交流协会第六次会议
1.立会照片 成员:王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:任思佳 2.时间跨度: 2017年11月22日 12:00 — 12:20,总计20分钟. 3.地点: 一食 ...
- c++中好用的函数
isdigit()返回是否是数字字符: isalpha()返回是否是字母字符: isupper()返回是否是大写字母: islower()返回是否是小写字母:
- ROS使用小知识点
输入 rosrun rqt_graph rqt_graph 可以打开一个界面观察节点与话题的关系 绿色和蓝色的是节点 红色的是话题 查看ros中额的tf转换信息 rosrun rqt_tf_tree ...