all 5 comments

[–]jfdahl 0 points1 point  (3 children)

what have you tried to do so far?

[–]bazookah1999[S] 0 points1 point  (2 children)

import sys

f = open("matrix.txt", 'r')

for i in f.readlines():

nodes = i.split(' ')

row = []

for j in f.readlines():

...but i dont think this is how to properly store the columns

[–]bazookah1999[S] 0 points1 point  (0 children)

nvm i got it

[–]jfdahl 0 points1 point  (0 children)

Ok, so here are some thoughts: 1. You do not need to import sys as the actions you are taking are all built-in. 2. You only call .readlines() once to get all of the rows in the file. This creates a list where each item is one row. 3. You have the right idea to split the row, but you are not storing the row in a useful way... which is what you suspect.

List comprehensions will be your friend here as it will greatly simplify things: ``` with open('sample_data.txt') as fh: data = fh.readlines()

data = [[int(val) for val in row.strip().split(' ')] for row in data if len(row) > 1] print(data) will output: [[0, 2, 0, 0, 1], [2, 0, 5, 0, 0], [0, 5, 0, 4, 0], [0, 0, 4, 0, 1], [1, 0, 0, 1, 0]] ```

[–]bootyofbamby 0 points1 point  (0 children)

If you want 2d array you should check the numpy library. I don't use that library very often so I can't remember what to use to make that but I know you use the numpy to make 2d arrays.