python中map函数的使用

from collections.abc import Iterator, Iterable
from icecream import ic
list1 = [1, 2, 3]
list2 = [3, 4, 5, 6, 7]


def func1(x):
    return x+1


def func2(x, y):
    return x+y


# Map集合是以 键值对<key , value> 保存数据,key值具有唯一性
# map()函数返回的是一个新的对象,不会改变原有对象。
a = map(func1, list1)  # 生成一个迭代器
"""
查看源码:
map(func, *iterables) --> map object
Make an iterator that computes the function using arguments from
each of the iterables.  Stops when the shortest iterable is exhausted.
"""
print(a)
ic(isinstance(a, Iterable))  # 可迭代
ic(isinstance(a, Iterator))  # 是迭代器
for i in a:  # 用for循环遍历迭代器,输出值
    print(i)
print(list(a))  # 遍历结束之后,迭代器即为空,无元素!!!

b = map(func2, list1, list2)  # func2有两个参数,故需传入两个生成器。
for i in b:
    print(i)  # Stops when the shortest iterable is exhausted.

list3 = [100, 100]
c = map(lambda x, y, z: x+y+z, list1, list2, list3)  # 结合lambda表达式使用 map函数
for i in c:
    print(i)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值