looping in array vs looping in a dic

giuseppe.amatulli at gmail.com giuseppe.amatulli at gmail.com
Thu Sep 20 19:35:15 EDT 2012


Hi Ian and MRAB
thanks to you input i have improve the speed  of my code. Definitely reading in dic() is faster. I have one more question.
In the dic() I calculate the sum of the values, but i want count also the number of observation, in order to calculate the average in the end. 
Should i create a new dic() or is possible to do in the same dic().
Here in the final code. 
Thanks Giuseppe
  


rows = dsCategory.RasterYSize
cols = dsCategory.RasterXSize

print("Generating output file %s" %(dst_file))

start = time()

unique=dict()

for irows in xrange(rows):
    valuesRaster=dsRaster.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
    valuesCategory=dsCategory.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
    for icols in xrange(cols):
        if ( valuesRaster[0,icols] != no_data_Raster ) and ( valuesCategory[0,icols] != no_data_Category ) :
            row = valuesCategory[0, icols],valuesRaster[0, icols]
            if row[0] in unique :
                unique[row[0]] += row[1]
            else:
                unique[row[0]] = 0+row[1] # this 0 was add if not the first observation was considered = 0




More information about the Python-list mailing list