how can i read and rewrite a matrix from another file in python? -
i try read matrix file can specific values , rewrite them. have file matrix of 10 10 , print it. how can specific numbers matrix?
this code open matrix:
f = open ( 'matrix.txt' , 'r') l = [] l = [ line.split() line in f] print(l)
this output:
[['0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,'], ['0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,'], ['0,', '0,', '0,', '0,', '3,', '3,', '0,', '0,', '0,', '0,'], ['0,', '0,', '0,', '0,', '3,', '3,', '0,', '0,', '0,', '0,'], ['0,', '0,', '3,', '3,', '2,', '2,', '3,', '3,', '0,', '0,'], ['0,', '0,', '3,', '3,', '2,', '2,', '3,', '3,', '0,', '0,'], ['0,', '0,', '0,', '0,', '3,', '3,', '0,', '0,', '0,', '0,'], ['0,', '0,', '0,', '0,', '3,', '3,', '0,', '0,', '0,', '0,'], ['0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,', '0,'], ['1,', '1,', '2,', '2,', '1,', '1,', '2,', '2,', '1,', '1,']]
you have 10x10 array of strings, can access by
p = l[i][j]
you string result, such l[4][5] = '2,'
if want access value, need convert number
p = int(l[4][5][:-1])
this set p = 2
Comments
Post a Comment