Biax
Biax

Reputation: 37

Decimal list index out of range

I have a file that represents a huge matrix:

54321

|  This     | Table     |           |
|:------:   |:-----:    |:-----:    |
| 6.75      | 0         | 20020     |
| 1         | 0         | 13663     |
| 107.75    | 0         | 0         |
| 0.25      | 1         | 27508     |
| 5.5       | 1         | 10964     |
| 11        | 1         | 19826     |
| 9         | 1         | 19817     |
| 7.75      | 1         | 27525     |
| 1.75      | 1         | 13005     |
| 5.25      | 1         | 2441      |
| 1.75      | 1         | 17250     |
| 142.25    | 1         | 1         |

where first line is dimension, second line is a tuple that looks like (elements from sparse matrix, line index, column index).

I have to read from this file the dimension and to generate vectors for matrix storage.

def getLineIndex(a, x):
for lineIndex in range(0, len(a)):
    diagonalValue, lineNumber = a[lineIndex][-1]
    if lineNumber == x:
        return lineIndex
return -1

def getColumnIndex(a, x, y):
line = a[x]
for i in range(0, len(line)):
    value, columnNumber = line[i]
    if columnNumber == y:
        return i
return -1

def read_values(filename):
with open(filename,'r') as file:
    n = int(file.readline())

    b = list()
    for i in range(0, n):
        b.append((file.readline()))

    a = list()
    for line in file:
        values = line.replace(",", " ").split()
        value = Decimal(values[0])
        x = int(values[1])
        y = int(values[2])

        lineIndex = getLineIndex(a, x)
        if lineIndex != -1:
            columnIndex = getColumnIndex(a, lineIndex, y)
            if columnIndex != -1:
                a[lineIndex][columnIndex][0] += value
            else:
#                 #addNewColumn(a, value, lineIndex, y)
                a[lineIndex].insert(0, [value, y])

But I receive this error:

File "source.py", line 31, in read_values value = Decimal(values[0]) IndexError: list index out of range

I appreciate any type of help.

Upvotes: 0

Views: 209

Answers (2)

awarrier99
awarrier99

Reputation: 3855

I think your line variable is empty, causing values to be empty, because you have already iterated through all the lines and put them into b, so now you are trying to read from the bottom of the file, which is likely reading empty lines. You probably want to seek back to the beginning of the file before iterating through each line again using something like this: file.seek(0, os). Once you are back at the top of the file, you can use @NanduRaj's solution to skip over the first line and iterate as you want.

Upvotes: 0

Nandu Raj
Nandu Raj

Reputation: 2110

In your first line, which is 54321, there is no space or ",". So splitting with space or "," returns None. Thus values array is empty. When you try to access values[0], this error will occur: IndexError: list index out of range. This should work:

for line in file:
    values = line.replace(",", " ").split()  //line number 30
    if len(values) == 0:
       continue
    value = Decimal(values[0])

Upvotes: 1

Related Questions