首页 > 解决方案 > IndexError:数组python的索引太多

问题描述

我的问题是从数组中获取除第一个元素之外的所有元素。我使用对象 p 和 q。

 print(p.p)
 print(q.p)

输出是:

(79, 12.37, 1.63, 2.3, 24.5, 88., 2.22, 2.45, 0.4, 1.9, 2.12, 0.89, 2.78, 342.)

(29, 12.33, 0.99, 1.95, 14.8, 136., 1.9, 1.85, 0.35, 2.76, 3.4, 1.06, 2.31, 750.)

如果我试试这个:

 x = p.p[1:]
 y = q.p[1:]

我收到此错误:

IndexError:数组的索引过多

我想我有这个错误,因为如果我尝试:

print(p.p(shape))

输出是:

()

我怎么解决这个问题 ?

更新:

class Point:

def __init__(self, p):
    self.p = p #numpy
    self.NN = [] 
    self.active = True


    

标签: pythonindexing

解决方案


只需创建一个for索引变量从 1 开始的循环:

for i in range(1, p.size):
    print(p[i])

推荐阅读