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 ...
随机推荐
- jmeter之headers中的Content-Type.
jmeter在发送http post请求时,会把body 里的数据格式给携带上(最好携带上),一般在信息头里增加.另因一篇文章.感觉写的非常好.https://imququ.com/post/fou ...
- Unity3D AssetBundle的打包与加载
在Unity项目开发过程中,当要做热更新时常常使用一个叫做AssetBundle的东西,这里做一点个人的学习记录 步骤1: 设置打包标签:具体步骤----进入Unity,选择某一资源然后看右下角,在那 ...
- 喝汤 beautifulsoup 批量爬取图片
未成功 from urllib.request import urlopen import re import random base_url = "http://www.meizitu.c ...
- .net 服务因为GC时遇到的问题和解决办法
1.问题: .net单一服务中,大量的请求访问后台服务,多线程处理请求,但每个线程都可能出现超时的现象.记录超时日志显示,超时可能在序列化时,Socket异步发送AsyncSend数据时,普通业务处理 ...
- 编译原理子cygwin的使用
目的:熟悉cygwin环境的使用,学习使用lex写简单的词法分析程序,会在cygwin环境下使用flex调试lex写的程序 内容:使用cygwin下的flex工具将exam1.l和exam2.l编译并 ...
- 设置Ubuntu右侧显示扩展屏幕。。。
sudo xrandr --output VGA1 --right-of LVDS1 --auto
- @RequestMapping、@ResponseBody和@RequestBody的使用
使用SSM框架进行Web开发时,经常在Controller中遇到@RequestMapping.@ResponseBody和@RequestMapping注解. 1.@RequsetMapping注解 ...
- [转]大白话讲解Promise(一)
http://www.cnblogs.com/lvdabao/p/es6-promise-1.html 去年6月份, ES2015正式发布(也就是ES6,ES6是它的乳名),其中Promise被列为正 ...
- CentOS无法使用ifconfig和root密码修改
初学Linux,总是有许多问题,这次就遇到了这个问题: 想使用ifconfig命令查看一下虚拟机的ip地址,结果发现ifconfig命令无法使用,总是显示找不到ifconfig这个命令. 上网查询帮助 ...
- [LeetCode&Python] Problem 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...