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

ShanZhengYang

我有一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

查找大于阈值的NumPy数组的第一个和最后一个元素

NumPy数组中的第一个和最后一个元素

从第k个最后一个元素到第k个第一个元素的numpy索引

查找未知形状的numpy ndarray的第一个元素

遍历Python列表,但第一个元素在末尾?

链表遍历会跳过第一个元素

第一个孩子最后一个孩子Dom遍历不起作用

使用密码语言从第一个节点遍历到最后一个节点

在querySelector中:如何获取第一个元素并获取最后一个元素?dom使用什么遍历顺序?

遍历numpy数组的最后一个维度

numpy:在排序列表中,找到每个唯一值的第一个和最后一个索引

如何在每行中获取 n 个最后/第一个 True 的 NumPy 数组

遍历2个数组仅循环第一个元素

NumPy:如何根据某些条件确定 ndarray 的第一个轴的索引?

遍历一个numpy数组

遍历一个numpy数组

遍历一个numpy数组

循环遍历 Div 中的元素仅获取第一个元素

遍历数组的每个元素(第一个元素除外)

获取NumPy数组中的连续命中数及其第一个/最后一个索引

减少ndarray的第一个维度-保留第一个和最后一个元素

通过第一个数组的元素连接两个 numpy 数组

遍历元组列表并仅解压缩第一个元素

遍历嵌套集合以找到与条件匹配的第一个子-子-子元素

在第一个元素上进行转换后立即停止遍历

是否可以遍历数组,但不包括第一个元素?

为什么C++中的foreach不能遍历第一个元素?

带有 selenium 的 Python:循环遍历列表中的第一个 Web 元素

Numpy max 仅在一对数组的第一个元素上