简体   繁体   English

索引错误:在python中使用numpy时,索引超出范围

[英]Index Error: Index out of bounds when using numpy in python

I have a code that works fine when I have small CSV's of data but errors out when I try to run large CSV's through it. 我有一个代码,当我有小的CSV数据时可以正常工作,但是当我尝试通过它运行大型CSV时却出错了。 In essence this code is supposed to place 3 CSV's worth of data into 3 separate dictionaries, combine those dictionaries into a master dictionary, and then preform arithmetic operations on dictionary. 从本质上讲,该代码应该将3个CSV的数据放入3个单独的词典中,将这些词典合并到主词典中,然后对词典执行算术运算。 The input CSV's look something like this: 输入的CSV如下所示:

time   A  B  C  D
  0    3  4  6  4
.001   4  6  7  8 
.002   4  6  7  3 

The code that I am using is the code displayed below. 我正在使用的代码是下面显示的代码。 The error occurs within the lines 47 and 65 where I am try to preform arithmetic with the dictionary. 错误发生在第47和65行中,在该行中我尝试使用字典执行算术运算。 Any explanation as to why this is going on is greatly appreciated. 对此原因的任何解释都将不胜感激。

import numpy

Xcoord = {}
time = []
with open ('Nodal_QuardnetsX2.csv', 'r') as f:
    f.readline() # Skips first line
    for line in f:
        values = [s.strip()for s in line.split(',')]
        Xcoord[values[0]] = map(float, values[1:])
        time.append(values[0])

Ycoord = {}
with open ('Nodal_QuardnetsY2.csv', 'r') as f:

    f.readline() # Skips first line
    for line in f:
        values = [s.strip()for s in line.split(',')]
        Ycoord[values[0]] = map(float, values[1:])

Zcoord = {}
with open ('Nodal_QuardnetsZ2.csv', 'r') as f:
    f.readline() # Skips first line
    for line in f:
        values = [s.strip()for s in line.split(',')]
        Zcoord[values[0]] = map(float, values[1:])

# Create a master dictionary of the form {'key':[[x, y, z], [x, y, z]}   
CoordCombo = {}
for key in Xcoord.keys():
    CoordnateList = zip(Xcoord[key], Ycoord[key], Zcoord[key])
    CoordCombo[key] = CoordnateList

counter = 0
keycount1 = 0
keycount2 = 0.001
difference = []
NodalDisplacements = {}

#Find the difference between the x, y, and z quardnets relative to that point in time
while keycount2 <= float(values[0]): 
        Sub = numpy.subtract(CoordCombo[str(keycount2)][counter], CoordCombo[str(keycount1)][counter])    
        counter = counter + 1
        difference.append(Sub)
        NodalDisplacements[keycount1] = Sub
        keycount1 = keycount1 + 0.001
        keycount2 = keycount2 + 0.001

counter = 0
keycount3 = 0
keycount4 = 0.001

Sum = []
breakpoint = float(values[0])-0.001

while keycount4 <= breakpoint:
    Add = numpy.sum(NodalDisplacements[keycount4][counter], NodalDisplacements[keycount3][counter])
    Sum.append(Add)
    keycount3 = keycount3 + 0.001
    keycount4 = keycount4 + 0.001
    counter = counter + 1
    if counter == 2:
        counter = 0
print Sum

probably a line of your csv file does not contain 5 elements or the line is empty. 您的csv文件中的一行可能不包含5个元素,或者该行为空。 In your logic I would suggest to use 按照您的逻辑,我建议使用

for line in f:
   line = line.strip()
   if not line: continue
   if len(values) != N_COLS: continue # or error...
   # other ...

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

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