给定两个数组,是否有一种 numpy 非循环方法来检查数组之间的每个第 i 个索引是否匹配,也就是检查每个 i 如果 a[i]==b[i]?
a = np.array([1,2,3,4,5,6,7,8])
b = np.array([2,3,4,5,6,7,8,9])
Output: 0 matches
Run Code Online (Sandbox Code Playgroud)
我希望这已经被问到,但我找不到我要找的东西,如果是的话,我很抱歉。
尝试这个:
np.arange(len(a))[a==b]
Run Code Online (Sandbox Code Playgroud)
它创建一个从 0 到a表示索引的长度的新数组。然后使用a==b对数组进行切片,返回其中a和b相同的索引。
另外来自@Reblochon-Masque:
您可以使用numpy.where提取两个值满足指定条件的索引:
import numpy
a = numpy.array([0, 1, 2, 3, 4, 5, 6])
b = numpy.array([6, 5, 4, 3, 2, 1, 6])
numpy.where(a==b)
Run Code Online (Sandbox Code Playgroud)
(array([3, 6]),)
Run Code Online (Sandbox Code Playgroud)
您可以使用numpy.where提取两个值满足指定条件的索引:
import numpy
a = numpy.array([0, 1, 2, 3, 4, 5, 6])
b = numpy.array([6, 5, 4, 3, 2, 1, 6])
numpy.where(a==b)
Run Code Online (Sandbox Code Playgroud)
(array([3, 6]),)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17061 次 |
| 最近记录: |