# Form n(i) x n(j) array and then append with "columnar" M(i,j) array
nn_arr = n[np.array(list(itertools.product(range(5), repeat=2)))]
nn_M_arr = np.concatenate((nn_arr,M.reshape(-1,1)),axis=1)
# Get linear indices version
dims = nn_M_arr.max(0)+1
lidx = np.ravel_multi_index(nn_M_arr.T,dims)
# Get unique indices and the counts
_, idx, counts = np.unique(lidx,return_index=True,return_counts=True)
# Get corresponding unique triplets using unique indices and zip with counts
out = zip(map(tuple,nn_M_arr[idx]),counts)
让:
做三倍并下降i=j:
unique的问题是它只使用1D数组。一个解决方案是在字符串中转换行:这样可以确保延迟比较,而且通常比较快。你知道吗
如果整数像这里这样小(<;4),则可以显示结果,以便
res[n(i),n(j),M(i,j)]
给出计数:由于数组包含整数,因此可以将每个三元组视为可线性索引的元素。这里有一种方法,它将哲学铭记在心,从而避免循环,就像这样-
样本运行-
相关问题 更多 >
编程相关推荐