Python——map内置函数学习

本文详细解析了Python内置函数map的工作原理及应用实例,包括如何将函数应用于迭代对象的每个元素,以及处理多个迭代对象时的行为特性。

先看手册中定义:

map(function, iterable, …)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

map()函数是常用的高阶函数,第一个参数是函数,第二个参数是迭代对象,功能是对可迭代对象分别使用函数,返回map对象。
map函数是python的内置函数之一,内置函数概览详见:https://mp.youkuaiyun.com/mdeditor/90751587#
https://blog.youkuaiyun.com/oaa608868/article/details/53506188

实例:

a= [1, 2, 3]
def f(x):
    return x**2
print(map(f,a))#output:map object at  0x0000024C36EC1C50
print(list(map(f,a)))#使用list实现强制转化,output:[1,4,9]

此时f函数不带参数;
含有多个可迭代对象时,最短的结束之后,迭代停止

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值