美文网首页
py3笔记12:map

py3笔记12:map

作者: _百草_ | 来源:发表于2023-05-23 11:21 被阅读0次

    map(func, *iterables) --> map object

    # map()
    """
    map(function,iterable) 根据提供的函数对指定序列做映射
    
    # 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.
    
    """
    a =map(abs,[-1,-2,3,4])  
    # iterable依次执行函数,获得函数返回值,返回map object(是一个迭代器)
    print(a) # # 返回一个迭代器 # <map object at 0x000002199636D1C0>
    print(list(a)) # 使用list转为列表 # [1, 2, 3, 4]
    
    b = map(lambda x,y:x+y,[-1,-2,-3,4],[1,3,5]) # 使用lambda表达式
    print(list(b)) # 使用最短的iterable  # [0, 1, 2]
    # 如果又多个迭代器,迭代器数量将为 min(len(iterator1), len(iterator2), ...)
    

    相关文章

      网友评论

          本文标题:py3笔记12:map

          本文链接:https://www.haomeiwen.com/subject/ktjxedtx.html