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

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -