我有两个不同大小的数组,想确定一个数组的元素在另一个数组中的位置。我希望能够允许元素之间的公差。目标应该是这样的:
array1 = [1, 2, 3, 4, 5, 6]
array2 = [2, 8, 1.00001, 1.1]
places = [mystery function](array1, array2, tolerance = 0.001)
返回array1
中的索引places = [0,1]
。
我能得到的最接近的是np.isin
,它允许不同大小和顺序的数组,但不允许公差。(也尝试过np.allclose
,但形状和顺序不匹配是一个问题)。当然,这可以用循环来完成,但我的实际数组有数千个元素长,所以循环是不实用的。
(it也不必是numpy函数--实际上只是比循环更有效的函数)
提前感谢您的帮助!
2条答案
按热度按时间k4emjkb11#
使用自定义函数查找2个阵列和符合给定公差的位置之间的绝对差异:
wd2eg0qa2#
虽然我不知道numpy中有什么函数,但我确实写了一个。
希望这对你有帮助: