函数编程-map()

本文通过实例详细介绍了Python中map函数的使用方法,包括如何将函数应用于一个或多个序列,并展示了当序列长度不一时的处理方式。同时,文章还探讨了使用lambda表达式作为map函数的第一个参数的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4]
>>> ll
[2, 4, 5, 6, 7, 8, 9, 4, 1, 5, 23, 45, 345, 56, 11, 3]
>>> help(map)
Help on built-in function map:

map(...)
    map(function, sequence[, sequence, ...]) -> list

    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).

>>> map(lambda x,y :x<y and x or 0,l,ll)
[1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 2, 3, 4, 0, 0, 0]
>>> map(None,l,ll)
[(1, 2), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8), (7, 9), (8, 4), (9, 1), (10, 5)
, (2, 23), (3, 45), (4, 345), (None, 56), (None, 11), (None, 3)]
>>> map(l,ll)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'list' object is not callable
>>> map(None,ll)
[2, 4, 5, 6, 7, 8, 9, 4, 1, 5, 23, 45, 345, 56, 11, 3]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值