Search code examples
pythonarrayslistmultidimensional-arraynested-loops

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.

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.

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!

(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
-30
1 0
-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"

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!


Solution

  • You're importing numpy, so you might as well use it :) Numpy arrays are much more efficient and versatile for this type of usage*

    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).

    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])
    

    *Lists are good when you don't know how big they're going to be, or what objects they will contain. But here, you know the shape and content, so it's good to use a numpy.array.