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!newsfeed6.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.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'mrab': 0.05; 'sep': 0.09; 'instead:': 0.16; 'looping': 0.16; 'numpy': 0.16; 'read:': 0.16; 'subject:array': 0.16; 'subject:looping': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'expanding': 0.27; 'message-id:@mail.gmail.com': 0.27; 'cat': 0.29; 'probably': 0.29; 'point': 0.31; 'to:addr:python-list': 0.33; 'operations': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'should': 0.36; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'skip:n 10': 0.63; '20,': 0.65; 'subject:dic': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=yOGh5F22wYMWPbExCsohapXdqnzyHxLB6Hn0T7nqE1I=; b=FLbWa57ytuEAp80lZhsgqW9b8iUbI3clOr8/f466tRD/zELL0hck1BUYhy/x0knDtx JfJI0j8FkvQMUIawWdkwIw1hfhrISFNTpQZO9jhgCxAMsZYwG0C9szygAPChjt0d8Cr2 msuHcA6xksWH5KHSVM1YrrVzEFO3ERVgiJnvj3i5+XMDmbQlHP03OuyFGle3LnEMi5W4 w51Ee7o5ZP6KC4dWZip0LP5HJ+wjunIZ0XaU5n4OzvhPzucU0zpiBsCgOe88hxAcUSo9 DSVehJml0bxXtlh3JNmR4Y2WHxMd60W+wBOq58onbhXQseeAF3yHn5reeVLbDGceRzbi NLOQ== MIME-Version: 1.0 In-Reply-To: References: <007b2d71-3355-4085-b84f-204834b2c8d0@googlegroups.com> <505B6A00.10308@mrabarnett.plus.com> From: Ian Kelly Date: Thu, 20 Sep 2012 13:29:23 -0600 Subject: Re: looping in array vs looping in a dic To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: , Newsgroups: comp.lang.python Message-ID: Lines: 14 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348169395 news.xs4all.nl 6944 [2001:888:2000:d::a6]:39204 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29571 On Thu, Sep 20, 2012 at 1:28 PM, Ian Kelly wrote: > Expanding on what MRAB wrote, since you probably have far fewer > categories than pixels, you may be able to take better advantage of > numpy's vectorized operations (which are pretty much the whole point > of using numpy in the first place) by looping over the categories > instead: > > for cat in categories: > totals[cat] += np.sum(valuesCategory * (valuesRaster == cat)) Of course, that should have read: for cat in categories: totals[cat] += np.sum(valuesRaster * (valuesCategory == cat))