how to fix output data from file so the matrix is stacked instead of in 1 line

48 views Asked by At
result = [[sum(a*b for a,b in zip(matrix1_row,matrix2_col)) for matrix2_col in zip(*matrix2)] for matrix1_row in matrix1]    

outf = open("multimatrix.txt", "w")
outf.write(str(result)[1:-1])
outf.close()

this gives me [1750, 1029], [2252, 754] in the output file however I want it to look like this

1750 1029

2252 754

im guessing its because of the way i did the matrix multiplication however i couldnt get numpy to work in thonny

1

There are 1 answers

0
swalters On

Here are 2 ways you can do this in python. First you can loop through your list of list and write each line to the file.

with open("multimatrix.txt", "w") as f:
    for line in result:
    f.write(str(line)[1:-1]+'\n')

Second you can create the string you want to write using list comprehension and write it all at once.

with open("multimatrix.txt", "w") as f:
    f.write('\n'.join([str(x)[1:-1] for x in result]))