Numpy 乘法并创建新列表 B[i] = Product(A[j]) where j =!一世
Numpy multiplication and create new list B[i] = Product(A[j]) where j =! i
创建一个一维数组 B,其中 B[i] 是所有 A[j] 的乘积,其中 j != i。
例如:如果 A = {2, 1, 5, 9},则 B 将为 {45, 90, 18, 10}
这是 A
作为数组的一种方式 -
In [59]: A2D = np.repeat(A[None],len(A),axis=0)
In [60]: np.fill_diagonal(A2D,1)
In [61]: A2D.prod(1)
Out[61]: array([45, 90, 18, 10])
或者用 np.prod
-
In [71]: A.prod()/A
Out[71]: array([45., 90., 18., 10.])
或者用 masking
-
In [85]: mask = ~np.eye(len(A),dtype=bool)
In [86]: np.broadcast_to(A,mask.shape)[mask].reshape(len(A),-1).prod(1)
Out[86]: array([45, 90, 18, 10])
创建一个一维数组 B,其中 B[i] 是所有 A[j] 的乘积,其中 j != i。
例如:如果 A = {2, 1, 5, 9},则 B 将为 {45, 90, 18, 10}
这是 A
作为数组的一种方式 -
In [59]: A2D = np.repeat(A[None],len(A),axis=0)
In [60]: np.fill_diagonal(A2D,1)
In [61]: A2D.prod(1)
Out[61]: array([45, 90, 18, 10])
或者用 np.prod
-
In [71]: A.prod()/A
Out[71]: array([45., 90., 18., 10.])
或者用 masking
-
In [85]: mask = ~np.eye(len(A),dtype=bool)
In [86]: np.broadcast_to(A,mask.shape)[mask].reshape(len(A),-1).prod(1)
Out[86]: array([45, 90, 18, 10])