python:map 函数

map(func, *iterables) --> map object


map()是 Python 内置的高阶函数,它接收一个函数 func 和一个 list(*iterables),并通过把函数 func 依次作用在 list 的每个元素上,得到一个新的 list 并返回。

例如,对于list [1, 2, 3, 4, 5, 6, 7, 8, 9]

如果希望把list的每个元素都作平方,就可以用map()函数:


map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])


注意:map()函数不改变原有的 list,而是返回一个新的 list。

python3:print(list(map)) #python3返回的是 map object

 

LeetCode:problem 771

sum(map(S.count, J))

#S.count : func ;

#J : *iterables ;

#map: 对J中每个元素进行S.count计数。

#sum([2,1,0,4,0]) = 7

posted @ 2018-08-29 19:13  Lemon_Rain  阅读(477)  评论(0编辑  收藏  举报