获取从索引到末尾的数组元素 [英] Get array elements from index to end

查看:59
本文介绍了获取从索引到末尾的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下数组:

import numpy as np
a = np.arange(1, 10)
a = a.reshape(len(a), 1)
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

现在,我想访问从索引4到末尾的元素:

Now, i want to access the elements from index 4 to the end:

a[3:-1]
array([[4],
       [5],
       [6],
       [7],
       [8]])

当我这样做时,生成的向量缺少最后一个元素,现在有五个元素而不是六个元素,为什么会发生,以及如何在不附加最后一个元素的情况下得到最后一个元素呢?

When i do this, the resulting vector is missing the last element, now there are five elements instead of six, why does it happen, and how can i get the last element without appending it?

预期输出:

array([[4],
       [5],
       [6],
       [7],
       [8],
       [9]])

预先感谢

推荐答案

[:-1] 删除最后一个元素.代替

The [:-1] removes the last element. Instead of

a[3:-1]

a[3:]

您可以在此处阅读Python切片符号:解释Python的切片符号

You can read up on Python slicing notation here: Explain Python's slice notation

NumPy切片是其扩展.NumPy教程涵盖了一些内容:索引,切片和迭代.

NumPy slicing is an extension of that. The NumPy tutorial has some coverage: Indexing, Slicing and Iterating.

这篇关于获取从索引到末尾的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆