查找 python 中两个数组之间互斥元素的索引

Find indices of mutually exclusive elements between two arrays in python

我有两个数组,我找到了如何用 np.setxor1d(a,b) 识别互斥元素。例如:

a = np.random.randint(11, size=10) #first array
b = np.random.randint(11, size=10) #second array
ex = np.setxor1d(a,b)              #mutually exclusive array

a
Out[1]: [1, 5, 3, 7, 6, 0, 10, 10, 0, 9]
b
Out[2]: [1, 9, 8, 6, 3, 5, 8, 0, 3, 10]
ex
Out[3]: [7, 8]

现在,我想弄清楚如何获取排他数组元素的索引,ex 用于 ab。以 a_mutex_indb_mutex_ind 等方式。有谁知道没有 for 循环的聪明方法吗? 谢谢!

>>> x = np.setxor1d(a, b)
>>> i, = np.nonzero(np.in1d(a, x))
>>> i
array([3])
>>> a[i]
array([7])

b 类似:

>>> j, = np.nonzero(np.in1d(b, x))
>>> j
array([2, 6])
>>> b[j]
array([8, 8])