Python如何计算两个数组中特定值的交集?

Python如何计算两个数组中特定值的交集?,python,tensorflow,Python,Tensorflow,我有两个数组A,B,它们的值都是[0,1,2](大小相同) 我想计算值1的索引交点。基本上换句话说,我想检查数组A上值1的精度 到目前为止,我已经尝试过map函数,但它没有工作 temp = list(map(lambda x,y: (x is y) == 1 ,A ,B)) 然而结果并不是我所期望的。你能给出一些关于如何解决这个问题的建议或例子吗?试试这个: x = np.array([0, 1, 2, 3, 1, 4, 5]) y = np.array([0, 1, 2, 4, 1, 3,

我有两个数组A,B,它们的值都是[0,1,2](大小相同) 我想计算值1的索引交点。基本上换句话说,我想检查数组A上值1的精度

到目前为止,我已经尝试过map函数,但它没有工作

temp = list(map(lambda x,y: (x is y) == 1 ,A ,B))
然而结果并不是我所期望的。你能给出一些关于如何解决这个问题的建议或例子吗?

试试这个:

x = np.array([0, 1, 2, 3, 1, 4, 5])
y = np.array([0, 1, 2, 4, 1, 3, 5])
print(np.sum(list(map(lambda x,y: (x==y==1) , x, y))))
输出:

2
tf.Tensor([False True False False True False False False True False False True False False], shape=(14,), dtype=bool)
tf.Tensor(4.0, shape=(), dtype=float32)
Tensorflow代码:

elems = (np.array([0, 1, 2, 3, 1, 4, 5, 0, 1, 2, 3, 1, 4, 5]), np.array([0, 1, 2, 4, 1, 3, 5, 0, 1, 2, 3, 1, 4, 5]))
alternate = tf.map_fn(lambda x: tf.math.logical_and(tf.equal(x[0], 1), tf.equal(x[0], x[1])), elems, dtype=tf.bool)
print(alternate)
print(tf.reduce_sum(tf.cast(alternate, tf.float32)))
输出:

2
tf.Tensor([False True False False True False False False True False False True False False], shape=(14,), dtype=bool)
tf.Tensor(4.0, shape=(), dtype=float32)

是的,这就是我要找的。非常感谢。