通过一个numpy ndarray迭代,管理第一个和最后一个元素

时间:2015-12-11 02:51:19

标签: python numpy compare

我有一个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]))

但这会引发错误。

我如何处理第一个和最后一个元素?

1 个答案:

答案 0 :(得分:2)

你应该避免NumPy中的循环。

在起始端使用带有对的略微修改的数组:

StudentID

这会找到每对的第一个索引。

>>> 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

由于我们有一维数组,我们得到一个元素的元组。对于2D数组,我们将得到一个包含两个元素的元组,沿着第一维和第二维保持索引。我们从元组中取出这些指数:

>>> cond = arr[:-1] == arr[1:]
>>> np.where(cond)
(array([ 0,  4,  6,  9, 11, 12, 16]),)