简体   繁体   English

遍历numpy ndarray,管理第一个和最后一个元素

[英]Iterate through a numpy ndarray, manage first and last elements

I have a numpy array 我有一个numpy数组

import numpy as np
arr = np.array([2, 3, 4, 7, 7, 4, 4, 5, 1, 1, 9, 9, 9, 4, 25, 26])

I would like to iterate over this list to produce pairs of "matching" elements. 我想遍历此列表以生成成对的“匹配”元素。 In the above array, 7 matches 7. You only compare the element "ahead" and the element "behind". 在上面的数组中,有7个匹配项7。您只能比较元素“ ahead”和元素“ behind”。

My problem: how do I deal with the first and last elements? 我的问题是:如何处理第一个和最后一个元素?

This is what I have to begin with: 这是我必须开始的事情:

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

This outputs: 输出:

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)

I feel the condition should be 我觉得情况应该是

 if ((arr[i] == arr[i+1]) and (arr[i] == arr[i-1]))

but this throws an error. 但这会引发错误。

How do I deal with the first and last elements? 如何处理第一个和最后一个元素?

You should avoid loops in NumPy. 您应该避免在NumPy中循环。

Using slightly modified array with pairs at the start end: 在开始端使用成对的稍微修改的数组:

>>> arr = np.array([2, 2, 3, 4, 7, 7, 4, 4, 5, 1, 1, 9, 9, 9, 4, 25, 26, 26])

This finds the first index of each pair. 这将找到每对的第一个索引。

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

Printing them out: 打印出来:

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

prints: 打印:

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)

The function np.where can be used in several ways. 函数np.where可以以多种方式使用。 In our case we use the condition arr[:-1] == arr[1:] . 在我们的例子中,我们使用条件arr[:-1] == arr[1:] This compares each element with the next in the array: 这会将每个元素与数组中的下一个元素进行比较:

>>> arr[:-1] == arr[1:]
array([ True, False, False, False,  True, False,  True, False, False,
        True, False,  True,  True, False, False, False,  True], dtype=bool)

Now applying np.where to this condition gives a tuple with matching indices. 现在将np.where应用于此条件将给出具有匹配索引的元组。

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

Since we have a 1D array, we get a tuple with one element. 由于我们具有一维数组,因此我们得到了一个包含一个元素的元组。 For a 2D array we would have gotten a tuple with two elements, holding the indices along the first and second dimension. 对于2D数组,我们将获得一个包含两个元素的元组,并沿第一维和第二维保存索引。 We take these indices out of the tuple: 我们从元组中删除这些索引:

>>> np.where(cond)[0]
array([ 0,  4,  6,  9, 11, 12, 16])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM