use*_*619 1 python arrays numpy multiplication
我是 numpy 的新手,并试图找到一种使用 numpy 编写多表的有效方法。
def mult_table():
result = []
for i in a:
for j in a:
result.append(i*j)
return result
Run Code Online (Sandbox Code Playgroud)
在 numpy 中,我看到一个点阵和一个 matmul,但不确定如何复制上述逻辑。
一种方法是使用numpy.arange. 您可以轻松地将其包装在一个函数中。
import numpy as np
def mult_table(n):
rng = np.arange(1, n+1)
return rng * rng[:, None]
print(mult_table(5))
# [[ 1 2 3 4 5]
# [ 2 4 6 8 10]
# [ 3 6 9 12 15]
# [ 4 8 12 16 20]
# [ 5 10 15 20 25]]
Run Code Online (Sandbox Code Playgroud)