简体   繁体   English

python中的嵌套for循环中的2D数组?

[英]2D array in a nested for loop in python?

I am new to python and have been trying to read in a data set from an excel file and store this in arrays/lists. 我是python的新手,一直在尝试从excel文件中读取数据集并将其存储在数组/列表中。

I am using "openpyxl" to handle the excel document so there is some non-standard syntax, however, all of that appears to be working and I believe the issue lies with the two dimensional array bit. 我正在使用“ openpyxl”来处理excel文档,因此存在一些非标准语法,但是,所有这些似乎都可以正常工作,我相信问题在于二维数组位。

It's probably a very basic rookie mistake, but as I'm used to working in C++ I'm having a lot of difficulty figuring it out! 这可能是一个非常基本的菜鸟错误,但是当我习惯于使用C ++时,要弄清楚它会遇到很多困难!

(I have included print statements for clarity and because that was how I was trying to troubleshoot) (为了清楚起见,我包含了打印语句,因为这是我尝试进行故障排除的方式)

%matplotlib inline  
    ​  
import numpy as np  
import matplotlib.pyplot as plt  
import math as mth  
import scipy as science  
from array import array  
​  
from openpyxl import load_workbook  
wb2 = load_workbook(r'C:"path and file name"')  
ws1 = wb2.active  
​  
timeArray = [None]*630  
voltageArray = [[None]*25,[None]*630]  
i=0  
j=0  
​  
​
for i in range (0, 625):  
    j=0  
    timeArray[i] = ws1.cell(row = i+1, column = 4).value  
    for j in range (0, 15):  
        voltageArray[j][i] =(ws1.cell(row = i+1, column = j+5).value)  
        print(j, i)  
        print(voltageArray[j][i])

Which prints out exactly; 确切地打印出来;
0 0 0 0
-30 -30
1 0 1 0
-29 -29
Which are the correct numbers, so far, but at this point it fails and gives the following error; 到目前为止,哪一个是正确的数字,但是此时它失败并给出以下错误;


IndexError                                Traceback (most recent call last)  
<ipython-input-9-fa1751a1a2f2> in <module>()  
     20     timeArray[i] = ws1.cell(row = i+1, column = 4).value  
     21     for j in range (0, 15):  
---> 22         voltageArray[j][i] =(ws1.cell(row = i+1, column=j+5).value)  
     23         print(j, i)  
     24         print(voltageArray[j][i]) 


IndexError: list index out of range

There is nothing in the spreadsheet that should cause this as the next cell is both filled and in the exact same format as the previous two. 电子表格中没有任何内容会导致这种情况,因为下一个单元格都被填充,并且格式与前两个单元格完全相同。

I assume there is something wrong with the way I have set up or am using the 2-dimensional array "voltageArray" 我认为我设置或使用二维数组“ voltageArray”的方式有问题

I have tried several fixes already from answers to similar questions but haven't been able to get any of them working. 我已经从类似问题的答案中尝试了几种修复方法,但是都无法使它们起作用。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You're importing numpy , so you might as well use it :) Numpy arrays are much more efficient and versatile for this type of usage* 您正在导入numpy ,因此您不妨使用它:)对于这种类型的使用,Numpy数组效率更高,用途更广泛*

The issue is that you're not creating voltageArray in the shape you expect, you're actually initializing it to be a list of two lists, the first inner-list has length 25, and the second inner-list has length 630. You actually want a 2D array with shape (25, 630). 问题是您没有voltageArray期望的形状创建voltageArray ,实际上是将其初始化为两个列表的列表,第一个内部列表的长度为25,第二个内部列表的长度为630。实际上想要一个形状为(25,630)的2D数组。

timeArray = np.zeros(625)    # Array of 625 zeros
voltageArray = np.zeros((25, 630))  # Array of zeros of shape (25,630)
for i in range(0, 625):  
    timeArray[i] = ws1.cell(row = i+1, column = 4).value  
    for j in range (0, 15):  
        voltageArray[j, i] =(ws1.cell(row = i+1, column = j+5).value)  
        print(j, i)  
        print(voltageArray[j, i])

* List s are good when you don't know how big they're going to be, or what objects they will contain. *当您不知道List的大小或包含的对象时, List会很好。 But here, you know the shape and content, so it's good to use a numpy.array . 但是在这里,您知道形状和内容,因此最好使用numpy.array

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

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