python代码:map函数
书《Python核心编程(第二版).pdf》,作者:Wesley J. Chun
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author: @file:test01.py @time:2022-01-11 16:24 """ """ 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. """ # map()的iterables为单个序列: # map()将函数调用“映射”到每个序列的元素上,并返回一个含有所有返回值的列表。 # result = map((lambda x: x + 2), [0, 1, 2, 3, 4, 5]) # 匿名函数 # [2, 3, 4, 5, 6, 7] # result = map(str, [0, 1, 2, 3, 4, 5]) # 指定函数 # ['0', '1', '2', '3', '4', '5'] # result = map(lambda x: x ** 2, range(6)) # 序列为函数 # [0, 1, 4, 9, 16, 25] # map()的iterables为多个序列: # map()会并行地迭代每个序列。 # # 在第一次调用时,map()会将每个序列的第一个元素捆绑到一个元组中,将func函数作用到map()上, # 当map()已经完成执行的时候,并将元组的结果返回到ma