python中map()函数使用方法详解
作者:IT之一小佬 时间:2022-12-19 13:56:37
先看map()函数底层封装介绍:
注释中翻译为:
map(func, *iterables)——> map对象
创建一个迭代器,使用来自的参数计算函数每个迭代器。当最短的迭代器耗尽时停止。
作用:
map(func, lst) ,将传⼊的函数变量 func 作⽤到 lst 变量的每个元素中,并将结果组成新的列表 (Python2)/ 迭代器(Python3) 返回。
注意:
map()返回的是一个迭代器,直接打印map()的结果是返回的一个对象。
示例代码1:
lst = ['1', '2', '3', '4', '5', '6']
print(lst)
lst_int = map(lambda x: int(x), lst)
# print(list(lst_int))
for i in lst_int:
print(i, end=' ')
print()
print(list(lst_int))
运行效果:
示例代码2:
lst = map(str, [i for i in range(10)])
print(list(lst))
lst_2 = map(str, range(5))
print(list(lst_2))
运行效果:
示例代码3:
list1 = [1, 2, 3, 4, 5]
def func(x):
return x ** 2
result = map(func, list1)
print(result)
print(list(result))
运行效果:
示例代码4:
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5, 6]
list3 = [1, 2, 3, 4, 5, 6, 7]
def func1(x, y, z):
return x + y + z
def func2(x, y, z):
return x, y, z
result1 = map(func1, list1, list2, list3)
print(result1)
print(list(result1))
result2 = map(func2, list1, list2, list3)
print(result2)
print(list(result2))
运行效果:
来源:https://blog.csdn.net/weixin_44799217/article/details/119799110
标签:python,map函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PyQt5实现画布小程序
2022-03-02 07:37:46
![](https://img.aspxhome.com/file/2023/1/99331_0s.jpg)
源码解析python中randint函数的效率缺陷
2023-01-24 01:23:54
栅格:灵活应变
2008-07-22 12:22:00
比较一下看看自己掌握了多少SQL快捷键
2009-01-04 14:04:00
Opencv图像添加椒盐噪声、高斯滤波去除噪声原理以及手写Python代码实现方法
2021-12-30 00:21:40
![](https://img.aspxhome.com/file/2023/9/88949_0s.jpg)
Numpy中创建数组的9种方式小结
2021-03-25 21:39:43
![](https://img.aspxhome.com/file/2023/7/103247_0s.png)
PHP中include和require的使用详解
2023-10-22 03:57:03
使用pycharm运行flask应用程序的详细教程
2022-12-15 05:15:55
![](https://img.aspxhome.com/file/2023/1/95221_0s.png)
Python API 操作Hadoop hdfs详解
2023-02-24 02:23:26
Python django框架应用中实现获取访问者ip地址示例
2022-01-06 08:08:20
![](https://img.aspxhome.com/file/2023/7/65047_0s.jpg)
javascript 45种缓动效果(二)
2009-09-19 18:53:00
![](https://img.aspxhome.com/file/UploadPic/up/2009091919271644.gif)
Numpy 多维数据数组的实现
2022-12-22 11:26:03
![](https://img.aspxhome.com/file/2023/3/97113_0s.png)
Python键鼠操作自动化库PyAutoGUI简介(小结)
2022-01-04 09:08:53
![](https://img.aspxhome.com/file/2023/0/81790_0s.png)
pyautogui自动化控制鼠标和键盘操作的步骤
2022-04-29 21:35:14
![](https://img.aspxhome.com/file/2023/2/101182_0s.png)
Python locust工具使用详解
2021-11-04 04:04:56
![](https://img.aspxhome.com/file/2023/3/83703_0s.png)
Python中if elif else及缩进的使用简述
2021-06-18 18:29:36
浅谈Python中的函数(def)及参数传递操作
2023-01-20 20:13:54
Python Request爬取seo.chinaz.com百度权重网站的查询结果过程解析
2022-12-01 07:16:59
![](https://img.aspxhome.com/file/2023/0/102930_0s.png)
对Django中static(静态)文件详解以及{% static %}标签的使用方法
2021-03-27 20:28:21
对fieldset和legend的默认样式一点分析
2008-07-02 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20087/2/2008721351157s.jpg)