I am trying to take a matrix presented in a .txt file and put it into a list. For example a 2x2 matrix would be in a list with the first sublist being the first row and the second sublist being the second row [[a,b],[c,d]]. I was trying to create a matrix list of a certain height by width and fill every value with a 0 and then update a small number of 0s to actual values.
m is a dictionary created from the txt file that looks like this:
{(2, 2): 5, (1, 2): 4, (0, 1): 2, (0, 0): 1, (1, 1): 3, (2, 3): 6}
r is the number of rows for the matrix, indicated in the text file. In this case r is 3
c is the number of columns. c is 4
s is a string for the name of the matrix that will be printed
def print_matrix(m,r,c,s):
w = sorted(m)
value_list = []
matrix_list = []
for i in range(c-1):
value_list.append(0)
for i in range(r):
matrix_list.append(value_list)
for i in w:
matrix_list[i[0]][i[1]] = m[i]
print(matrix_list)
After reading this topic: Changing an element in one list changes multiple lists ..?. I realize that when I try to edit the list of full of zeroes it changes a value for all rows, not just one row because the sublists are identical. How can I got about creating unique sublist that can be uniquely edited but preserve the ability for the list to have unique number of sublists, and sublist length to correspond to rows and columns?