Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'output': 0.04; 'mrab': 0.05; '%s"': 0.07; 'collections': 0.09; 'observation': 0.09; 'rows': 0.09; 'skip:v 70': 0.09; 'defaultdict': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'instead:': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'row': 0.16; 'simple.': 0.16; 'skip:v 60': 0.16; 'subject:array': 0.16; 'subject:looping': 0.16; 'wrote:': 0.17; 'input': 0.18; 'code.': 0.20; 'question.': 0.20; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'received:192.168.1.3': 0.29; 'file': 0.32; 'received:84': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'thanks': 0.34; 'add': 0.36; 'but': 0.36; 'should': 0.36; 'possible': 0.37; 'skip:v 20': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'skip:n 10': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.63; 'here': 0.65; 'sum': 0.66; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'raster': 0.84; 'reply-to:addr:python.org': 0.84; 'subject:dic': 0.84; 'faster.': 0.91; 'average': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=asgw+FlV c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=AAvI7MrX_rgA:10 a=w7zqQIw9OgQA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=nTz416_S3dcA:10 a=pGLkceISAAAA:8 a=lh-EWxt6mCyF4nP2WQsA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=BS-JHms1EA6ew-4b:21 a=f6oXfQaS3mrghP3A:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 21 Sep 2012 00:58:03 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: looping in array vs looping in a dic References: <007b2d71-3355-4085-b84f-204834b2c8d0@googlegroups.com> <505B6A00.10308@mrabarnett.plus.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348185486 news.xs4all.nl 6855 [2001:888:2000:d::a6]:47557 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29587 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