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 ...
随机推荐
- org.springframework.cloud FeignInterceptor
package org.rx.feign; import org.apache.commons.lang3.ArrayUtils; import org.aspectj.lang.Proceeding ...
- winform devexpress 用法汇总
废话不多说先上图 1.封装分页控件 qrcodeOnPage1.SearchData(gridControl2, IDataPage, sWhere, "", "tb_o ...
- SmartBinding与kbmMW#1
即将发布的kbmMW,实现了SmartBinding,SmartBinding的设计目标是: 必须易于使用 必须最小化或完全删除锅炉板代码.(你看到这里的趋势了吗?... kbmMW从那时开始就是为了 ...
- Map、Set、List区别
转:https://www.cnblogs.com/jing99/p/6947549.html 提到集合之前,先说说数组Array和集合的区别: (1)数组是大小固定的,并且同一个数组只能存放类型 ...
- 洛谷p1181 数列分段section I
#include<iostream> #include<vector> #include<algorithm> using namespace std; int M ...
- SpringBoot+POI报表批量导出
由于servletResponse 获取的输出流对象在一次请求中只能输出一次,所以要想实现批量导出报表,需要将excel文件打包成zip格式然后输出. 好了,废话不多说,上代码. 1. 首先,需要导入 ...
- entrySet用法 以及遍历map的用法
entrySet用法 以及遍历map的用法 keySet是键的集合,Set里面的类型即key的类型entrySet是 键-值 对的集合,Set里面的类型是Map.Entry 1.keySet( ...
- [转载]JS中 map, filter, some, every, forEach, for in, for of 用法总结
转载地址:http://codebay.cn/post/2110.html 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5] ...
- Canvas 渲染模式
1. Canvas Canvas Component 是UI布局和渲染的抽象空間,所有的UI都必須在此元素之下(子物件),简单来说 Canvas 就是渲染 UI 的組件. 2. Render Mode ...
- centos7 - mongodb3.6.5-配置文件
创建Linux管理员账号admin, 并加入wheel组,对mongodb, php等进行统一管理~ systemLog:destination: filelogAppend: truepath: / ...