网站首页 > 基础教程 正文
map()函数是Python中的一个内置函数,它的功能是将指定的函数,依次作用于可迭代对象的每个元素,并返回一个迭代器对象,执行效率极高,在日常工作环境中强烈推荐使用,本篇主要介绍Python map函数()的基本用法
一、map的基本用法(单参数)
# -*- coding:utf-8 -*-
datas = ['张飞','关羽','刘备']
def hello(name):
msg = f'Hello:{name}'
return msg
for data in map(hello, datas):
print(data)
运行结果
Hello:张飞
Hello:关羽
Hello:刘备
二、多参数
# -*- coding:utf-8 -*-
datas = ['张飞','关羽','刘备']
age = [20,21,22]
def hello(name,age):
msg = f'Hello:{name}, age: {age}'
return msg
for data in map(hello, datas,age):
print(data)
运行结果
Hello:张飞, age: 20
Hello:关羽, age: 21
Hello:刘备, age: 22
猜你喜欢
- 2024-11-02 Python函数式编程之map/reduce/filter进阶
- 2024-11-02 Python中map函数的奇淫技巧:优化你的编程体验
- 2024-11-02 python内置函数map/reduce/filter
- 2024-11-02 Python 内置函数与匿名函数 python匿名内部类
- 2024-11-02 第八篇:Python中函数介绍 python中的各种函数
- 2024-11-02 「每天3分钟学Python」Python中的 Map 和 Reduce
- 2024-11-02 Python中starmap有什么用的? python中start用法
- 2024-11-02 详解Python中的map、lambda和apply用法
- 2024-11-02 Python编程技巧:如何用Map, Filter, Reduce代替For循环?
- 2024-11-02 python lambda函数与map()、filter()、reduce()函数用法
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)