获取numpy数组的正确索引

getting correct index of numpy array

我必须成对比较 np.arrays,我需要返回索引。

我的代码是:

import numpy as np
Vals = np.array([[1.0, 1.0], [2., 2.], [1., 2.], [2., 1.], [3., 3.], [3., 3.]])
for Val in itertools.combinations(Vals,2):
    X1 = Val[0][0]
    X2 = Val[1][0]
    Y1 = Val[0][1]
    Y2 = Val[1][1]
    Index1 = np.where( (Vals == Val[0]).all(axis=1))[0][0]
    Index2 = np.where( (Vals == Val[1]).all(axis=1))[0][0]
    
    print(X1,Y1,Index1)
    print(X2,Y2,Index2)

这运行良好,直到两个或更多具有相同值的元组在 Vals 中(如示例中所示)。 np.where 返回这个元组在数组中的第一次出现,所以我得到了错误的索引。 如何获得正确的索引?

您是否尝试过使用 zip 串联遍历每个数组

for i, j in itertools.combinations(range(len(Vals)), 2):
    print(Vals[i], i, "|", Vals[j], j)
[1. 1.] 0 | [2. 2.] 1
[1. 1.] 0 | [1. 2.] 2
[1. 1.] 0 | [2. 1.] 3
[1. 1.] 0 | [3. 3.] 4
[1. 1.] 0 | [3. 3.] 5
[2. 2.] 1 | [1. 2.] 2
[2. 2.] 1 | [2. 1.] 3
[2. 2.] 1 | [3. 3.] 4
[2. 2.] 1 | [3. 3.] 5
[1. 2.] 2 | [2. 1.] 3
[1. 2.] 2 | [3. 3.] 4
[1. 2.] 2 | [3. 3.] 5
[2. 1.] 3 | [3. 3.] 4
[2. 1.] 3 | [3. 3.] 5
[3. 3.] 4 | [3. 3.] 5