lst = ['1', '2', '3', '4', '5', '6'] print(lst) lst_int = map(lambda x: int(x) ** 2, lst) print(list(lst_int)) # [1, 4, 9, 16, 25, 36] lst = map(str, [i for i in range(10)]) print(list(lst)) # ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] lst_2 = map(int, range(5)) print(list(lst_2)) # [0, 1, 2, 3, 4] list1 = [1, 2, 3, 4, 5] def func(x): return x ** 2 result = map(func, list1) print(result) # <map object at 0x0000000003DE5518> print(list(result)) # [1, 4, 9, 16, 25] list1 = [1, 2, 3, 4, 5] list2 = [1, 2, 3, 4, 5, 6] list3 = [1, 2, 3, 4, 5, 6, 7] def func1(x, y, z): return x + y + z def func2(x, y, z): return x, y, z result1 = map(func1, list1, list2, list3) print(result1) # <map object at 0x0000000003DE5C88> print(list(result1)) # [3, 6, 9, 12, 15] result2 = map(func2, list1, list2, list3) print(result2) # <map object at 0x0000000003DE5940> print(list(result2)) # [(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5)]
map( )会根据提供的函数对指定序列做映射
map(func, *iterables) 创建一个迭代器,用来自序列的参数依次传入函数。当序列迭代器耗尽时停止。
map()返回的是一个迭代器,直接打印map()的结果是返回的一个对象
标签:map,映射,int,list1,list,print,lst,序列 From: https://www.cnblogs.com/sangern/p/17414839.html