python匿名函數(lambda)

與正常寫法相比,使用匿名函數相當簡潔

  • map() 遍歷所有
<code>a = [1, 2, 3]
x = []
for each in a:
x.append(each+1)

print(x)
### 使用map(func, iterable)
print(list(map(lambda x: x+1, a)))/<code>
  • reduce(func, seq) 積累每次計算的值
<code>def num(x, y):
return x + y
print(reduce(num, [1, 2, 3, 4]))
--------------------------
print(reduce(lambda x, y: x*y, [1, 2, 3, 4]))/<code>
  • filter(func, iterable) 過濾滿足條件的值

<code>print(list(filter(lambda x: x%2==0,range(10))))/<code>


python匿名函數(lambda)


分享到:


相關文章: