lambda函數,簡化了函數定義的書寫形式,使代碼更為簡潔,但是使用自定義函數的定義方式更為直觀,易理解
g = lambda x:x+1 #上面的lambda表達式相當於下面的自定義函數 def gg(x): return x+1
map函數的原型是map(function,iterable,...),它的返回結果是一個列表
參數function傳的是一個函數名,可以是python內置的,也可以是自定義的
參數iterable傳的是一個可迭代的對象,例如列表,元組,字符串這樣的
map函數通常和lambda函數一起使用
這個函數的意思就是將function應用於iterable的每一個元素,結果以列表的形式返回,iterable后面還有省略號,意思就是可以傳很多個iterable,如果有額外的iterable參數,並行的從這些參數中取元素,並調用function,如果一個iterable參數比另外的iterable參數要短,將以None擴展該參數元素
a=(1,2,3,4,5) b=[1,2,3,4,5] c="zhangkang" la=map(str,a) lb=map(str,b) lc=map(str,c) print(la) print(lb) print(lc) 輸出: ['1', '2', '3', '4', '5'] ['1', '2', '3', '4', '5'] ['z', 'h', 'a', 'n', 'g', 'k', 'a', 'n', 'g']
print(map(lambda x: x * 2 + 10, foo)) #用for循環代替map print([x * 2 + 10 for x in foo])
參考
https://blog.csdn.net/csdn15698845876/article/details/73321593
https://www.jianshu.com/p/9f306285a3ca
