备忘录


In [1]:
g = lambda x:x+1
print(g(1))
print(g(2))


2
3

In [2]:
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]

In [3]:
print(list(filter(lambda x: x % 3 == 0, foo)))


[18, 9, 24, 12, 27]

In [4]:
print([x for x in foo if x % 3 == 0])  # 如果可以使用for...in...if来完成的,坚决不用lambda。


[18, 9, 24, 12, 27]

In [5]:
print(list(map(lambda x: x * 2 + 10, foo)))


[14, 46, 28, 54, 44, 58, 26, 34, 64]

In [6]:
print([x * 2 + 10 for x in foo])  # 如果可以使用for...in...if来完成的,坚决不用lambda。


[14, 46, 28, 54, 44, 58, 26, 34, 64]

In [7]:
from functools import reduce 
print(reduce(lambda x, y: x + y, foo))


139

In [8]:
import numpy as np

# 样本数据
coords1 = [1, 2, 3]
coords2 = [4, 5, 6]
np_c1 = np.array(coords1)
np_c2 = np.array(coords2)

# NumPy 内建函数
np.linalg.norm(np_c1 - np_c2)


Out[8]:
5.196152422706632

In [ ]: