我有一个numpy数组
import numpy as np
arr = np.array([2, 3, 4, 7, 7, 4, 4, 5, 1, 1, 9, 9, 9, 4, 25, 26])
我想遍历此列表以生成成对的“匹配”元素。在上面的数组中,有7个匹配项7。您只能比较元素“ ahead”和元素“ behind”。
我的问题是:如何处理第一个和最后一个元素?
这是我必须开始的事情:
for i in range(len(arr)):
if (arr[i] == arr[i+1]):
print( "Match at entry %d at array location (%d)" % (arr[i], i))
else:
pass
输出:
Match at entry 7 at array location (3)
Match at entry 7 at array location (4)
Match at entry 4 at array location (6)
Match at entry 1 at array location (9)
Match at entry 9 at array location (11)
Match at entry 9 at array location (12)
我觉得情况应该是
if ((arr[i] == arr[i+1]) and (arr[i] == arr[i-1]))
但这会引发错误。
如何处理第一个和最后一个元素?
您应该避免在NumPy中循环。
在开始端使用成对的稍微修改的数组:
>>> arr = np.array([2, 2, 3, 4, 7, 7, 4, 4, 5, 1, 1, 9, 9, 9, 4, 25, 26, 26])
这将找到每对的第一个索引。
>>> np.where(arr[:-1] == arr[1:])[0]
array([ 0, 4, 6, 9, 11, 12, 16])
打印出来:
arr = np.array([2, 2, 3, 4, 7, 7, 4, 4, 5, 1, 1, 9, 9, 9, 4, 25, 26, 26])
matches = np.where(arr[:-1] == arr[1:])[0]
for index in matches:
for i in [index, index + 1]:
print("Match at entry %d at array location (%d)" % (arr[i], i))
印刷:
Match at entry 2 at array location (0)
Match at entry 2 at array location (1)
Match at entry 7 at array location (4)
Match at entry 7 at array location (5)
Match at entry 4 at array location (6)
Match at entry 4 at array location (7)
Match at entry 1 at array location (9)
Match at entry 1 at array location (10)
Match at entry 9 at array location (11)
Match at entry 9 at array location (12)
Match at entry 9 at array location (12)
Match at entry 9 at array location (13)
Match at entry 26 at array location (16)
Match at entry 26 at array location (17)
该功能np.where
可以通过多种方式使用。在本例中,我们使用条件arr[:-1] == arr[1:]
。这会将每个元素与数组中的下一个元素进行比较:
>>> arr[:-1] == arr[1:]
array([ True, False, False, False, True, False, True, False, False,
True, False, True, True, False, False, False, True], dtype=bool)
现在应用于np.where
此条件将给出具有匹配索引的元组。
>>> cond = arr[:-1] == arr[1:]
>>> np.where(cond)
(array([ 0, 4, 6, 9, 11, 12, 16]),)
由于我们有一个一维数组,所以我们得到一个包含一个元素的元组。对于2D数组,我们将获得一个包含两个元素的元组,并沿第一维和第二维保持索引。我们从元组中删除这些索引:
>>> np.where(cond)[0]
array([ 0, 4, 6, 9, 11, 12, 16])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句