随笔_Python map()函数

map() 会根据提供的函数对指定序列做映射。

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

列表转换字符串输出测试:

 1 list1 = ['abc', 'DEF', 123]
 2 list2 = map(str, list1)
 3 # 使用str() 把列表内的元素转换成字符类型
 4 list3 = " ".join(list2)
 5 # 空格隔开
 6 print(type(list1), list1)
 7 print(type(list2), list2)
 8 print(type(list3), list3)
 9 # 输出结果
10 # <class 'list'> ['abc', 'DEF', 123]
11 # <class 'map'> <map object at 0x0000017740730C50>
12 # <class 'str'> abc DEF 123

Python 2.x 返回列表。

Python 3.x 返回迭代器。

猜你喜欢

转载自www.cnblogs.com/Raine/p/10177250.html