map() 根據提供的函數對指定序列做映射。
map() 函數語法:map(function, iterable, ...) function -- 函數 iterable -- 一個或多個序列
返回值:Python 2.x 返回列表 Python 3.x 返回迭代器,需轉為list才能輸出
# 兩列表相加,lambda與map函數
print(list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))) # python3的map函數返回對象,需轉為list ---> [3, 7, 11, 15, 19]
# 列表中的數字轉為字符串
list1 = [20, 16, 10, 5]
方法一:print(list(map(str,list1))) # 列表中的數字轉為字符串['20', '16', '10', '5']
方法二:list2 = [str(i) for i in list1] print(list2)
方法三:
list2 = []
for i in list1:
list2.append(str(i))
print(list2)
轉發: