Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29585

Re: looping in array vs looping in a dic

Newsgroups comp.lang.python
Date 2012-09-20 16:35 -0700
References <007b2d71-3355-4085-b84f-204834b2c8d0@googlegroups.com> <505B6A00.10308@mrabarnett.plus.com> <CALwzid=HowMzWmgiU2+2Gu4opxWu0Hx-iOFRZZcXi=BAdKVfrw@mail.gmail.com> <mailman.971.1348169395.27098.python-list@python.org>
Subject Re: looping in array vs looping in a dic
From giuseppe.amatulli@gmail.com
Message-ID <mailman.982.1348184123.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

looping in array vs looping in a dic giuseppe.amatulli@gmail.com - 2012-09-20 11:31 -0700
  Re: looping in array vs looping in a dic MRAB <python@mrabarnett.plus.com> - 2012-09-20 20:09 +0100
  Re: looping in array vs looping in a dic Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-20 13:28 -0600
  Re: looping in array vs looping in a dic Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-20 13:29 -0600
    Re: looping in array vs looping in a dic giuseppe.amatulli@gmail.com - 2012-09-20 16:35 -0700
      Re: looping in array vs looping in a dic MRAB <python@mrabarnett.plus.com> - 2012-09-21 00:58 +0100
    Re: looping in array vs looping in a dic giuseppe.amatulli@gmail.com - 2012-09-20 16:35 -0700

csiph-web