Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'output': 0.04; 'mrab': 0.05; '%s"': 0.07; 'observation': 0.09; 'rows': 0.09; 'skip:v 70': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'row': 0.16; 'skip:v 60': 0.16; 'subject:array': 0.16; 'subject:looping': 0.16; 'input': 0.18; 'code.': 0.20; 'question.': 0.20; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'file': 0.32; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'received:209.85': 0.35; 'add': 0.36; 'but': 0.36; 'should': 0.36; 'possible': 0.37; 'skip:v 20': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'received:209.85.214': 0.39; 'from:no real name:2**0': 0.60; 'skip:u 10': 0.60; 'first': 0.61; 'skip:n 10': 0.63; 'more': 0.63; 'here': 0.65; 'sum': 0.66; 'received:209.85.214.184': 0.84; 'received:mail- ob0-f184.google.com': 0.84; 'subject:dic': 0.84; 'faster.': 0.91; 'average': 0.93 Newsgroups: comp.lang.python Date: Thu, 20 Sep 2012 16:35:15 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=137.216.206.11; posting-account=X69PegoAAADSdW7HHxG-TPWTj8wMt3Gs References: <007b2d71-3355-4085-b84f-204834b2c8d0@googlegroups.com> <505B6A00.10308@mrabarnett.plus.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 137.216.206.11 MIME-Version: 1.0 Subject: Re: looping in array vs looping in a dic From: giuseppe.amatulli@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348184123 news.xs4all.nl 6986 [2001:888:2000:d::a6]:37572 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29585 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