I have a text file that contains more than one matrix like this enter image description here
I want to read this input file in python and store it in multiple matrices like:
matrixA = [...] # first matrix
matrixB = [...] # second matrix
...
so on. I know how to read external files in python but don't know how to divide this input file in multiple matrices, how can I do this?
CodePudding user response:
You can make a matrix of matrixes (also called a 2D matrix), and so on.
If you want to put all the input matrixes inside of a matrix you would do
Matrixes = [][]
Matrixes[0] = matrixA
Matrixes[1] = matrixB
CodePudding user response:
Get the total number of matrices n from the first line. For each iteration in n read using readline() till you encounter the matrix metadata triplet which is in the format (isalpha(), isnumeric(), isnumeric()). You can extract the name, the number of rows and columns and then traverse the following matrix through a for loop. Do this each time you encounter the above matrix metadata triplet.
Something like this
for each_matrix in n:
name, rows, cols = 'A', 3, 3
for _ in range(rows):
each_row = list(map(int,input().strip().split()))[:cols] # take space-separated rows of length cols
matrix_A.append(each_row) # append each row to the matrix