[GRASS-dev] python: get results of r.stats call into list of lists of numericals instead of list of strings

Moritz Lennert mlennert at club.worldonline.be
Fri Feb 12 14:00:02 PST 2016


Hi,

I need some help with python scripting. I'm running r.stats on two 
raster maps such as this:

         rstats_results  = gscript.read_command('r.stats',
                              input_=[a, b],
                              flags='n1',
                              separator='comma',
                              quiet=True)
         results = rstats_results.splitlines()


This gives me

['123,456', '456,789', '987,321']

Does anyone know an efficient way to transform this into a list of lists 
of numbers, such as:

[[123, 456], [456, 789], [987, 321]]

I know I can do this as follows:

mylist = []
for line in results:
	mylist.append([int(line.split(',')[0]), int(line.split(',')[1])])

But for very large raster maps this does not seem very efficient.

Is there a way of getting the output directly as a list of lists ? Or a 
more efficient transformation ?

Moritz


More information about the grass-dev mailing list