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


Groups > comp.lang.python > #29569

Re: looping in array vs looping in a dic

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 <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'mrab': 0.05; 'sep': 0.09; '1:09': 0.16; 'col': 0.16; 'instead:': 0.16; 'looping': 0.16; 'numpy': 0.16; 'range(cols):': 0.16; 'row': 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; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; '20,': 0.65; 'ras': 0.84; '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=LR9mulpnFmMnPLfXPHwsjgK8zfcR+dchcvzslhr9yco=; b=YbMXjBOjru+EVR+MaXAclPCUJQAsGStoSsb/Fb/1WIGl2xwzAH+4b+2vxwIkWLgzX4 fUG/hOvSJyo0fjA+Ikqp2VsawV2ZDBEsJTXvHlk9mjkhQUtgnOueEGeUlbIki3esDW72 +HvgM2dMDZ9CXJDsOI0vbG1jt7WjArBC14H6+dRmppKEtQOA5V7aN2QxePoxs9YKp2ae xRuy+laQ3vtxvjtKwRmB4yLaIRxAHO0n+Kjtw8ozJuRFeYq0PqyQpbh+BDQpNoKuu5+b byfZMcdw/kl/tZkw1SHujC2jMspUnn9SIcveE71YswTgv0YipjxcjD9B2P6KbDQmyBie Ctmw==
MIME-Version 1.0
In-Reply-To <505B6A00.10308@mrabarnett.plus.com>
References <007b2d71-3355-4085-b84f-204834b2c8d0@googlegroups.com> <505B6A00.10308@mrabarnett.plus.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 20 Sep 2012 13:28:12 -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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.969.1348169324.27098.python-list@python.org> (permalink)
Lines 15
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348169324 news.xs4all.nl 6961 [2001:888:2000:d::a6]:37704
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29569

Show key headers only | View raw


On Thu, Sep 20, 2012 at 1:09 PM, MRAB <python@mrabarnett.plus.com> wrote:
> for col in range(cols):
>     for row in range(rows):
>         cat = valuesCategory[row, col]
>         ras = valuesRaster[row, col]
>         totals[cat] += ras

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))

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