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


Groups > comp.lang.python > #29587

Re: looping in array vs looping in a dic

Date 2012-09-21 00:58 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: looping in array vs looping in a dic
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> <f375e37c-d700-4ca5-b06b-2d195a5644de@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.984.1348185486.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 2012-09-21 00:35, giuseppe.amatulli@gmail.com wrote:
> 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
>
Keep it simple. Use 2 dicts.

>
>
> 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
>
You could use defaultdict instead:

from collections import defaultdict

unique = defaultdict(int)
...
              category, raster = valuesCategory[0, icols], 
valuesRaster[0, icols]
              unique[category] += raster

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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