首页 > 解决方案 > removing an IndexError on python

问题描述

To start off, I'm basically a beginner in python and coding since its the first programming language that I've learned so if y'all respond could you please simplify what you're trying to say, thanks.

So I have a code that keeps outputing an IndexError: list index out of range error message and yet it also gives me the output I'm looking for.

Here's the code:

def read_voltages(f_name):
    f = open(f_name, "r")
    #  list comprehension
    #  inside np.array(), builds nested lists, separating each nested list by " " and \n for each line it reads
    data = np.array([line.strip("\n").split(" ") for line in f.readlines()])
    data_t = data.T
    return data_t


def rms(v, N):
    RMS = np.sqrt((1 / N) * ((v**2).sum()))
    return RMS


f_name = input("Enter file name(eg. file1.txt): ")
data_t = read_voltages(f_name)

#  list comprehension ~ converts strings in original output to integer values
#  inner comp builds list of int from sequence of valid objects in row
#  outer comp builds list of results of inner comp applied to each term in data_t
int_list = [[int(column) for column in row] for row in data_t]
v = int_list

print("Voltage values: \n", v)
print()

size = len(v[0])
for i in range(size):
    if size > 0:
        N = size
        rms_val = rms(np.array(v[i]), N)
        print(rms_val)
    elif size == 0:
        print(None)

And here's the output:

Enter file name(eg. file1.txt): voltages_test.txt
Traceback (most recent call last):
  File "C:\Users\amand\Documents\Voltages.py", line 79, in <module>
    rms_val = rms(np.array(v[i]), N)
IndexError: list index out of range
Voltage values: 
 [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 2, 3, 4, 5, 6, 7, 8, 9, 10], [3, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

6.2048368229954285
6.228964600958975
6.268971207462992

Process finished with exit code 1

For reference heres the .txt file being used:

1 2 3
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10

Like I said before, the code outputs what I want, but it gives me the IndexError and I don't want that since I need to add more code to finish the program I'm working on. When I removed the [i] in rms_val = rms(np.array(v[i]), N) it gave me a repeated set of 10 identical numbers which were calculated incorrectly. I'm not really sure what else to move around and am quite stuck on this :/.

If anyone can help that'd be great, thanks.

edit: The code is supposed to take in values for "v" which are taken in from a transposed list that comes from a user inputted .txt file. Then, it uses the v values to calculate the root-mean-square and outputs the values in a table of sorts, but that part I can manage.

标签: python

解决方案


You are defining your loop variable according to the length of v[0]

size = len(v[0])

Later in the code you use this variable to loop through v

rms_val = rms(np.array(v[i]), N)

The length of v len(v) is shorter than the len of v[0] len(v[0]) thus you are getting the error.

Make sure that you use the appropriate length when looping through your data.


推荐阅读