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


Groups > comp.lang.python > #47361

Re: Idiomatic Python for incrementing pairs

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'that?': 0.05; 'subject:Python': 0.06; '[0,': 0.09; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'alpha': 0.16; 'sat,': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'tim': 0.29; 'message-id:@mail.gmail.com': 0.30; 'chase': 0.31; 'received:google.com': 0.35; 'there': 0.35; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'holding': 0.65; 'temporary': 0.65; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=HKr9hqtBoEYl2O/mOM1MUFqMjB8oUxZRDWUplqUHYzM=; b=I0wa9f5CAaX1e0zoDP9SLI4KI1gT0JF0KkURJDOP4o9CERVIuPJbqEC9Svg2U1AOxB HhPV+Rha0Ew4ErlUnp3RrUBYLPP4ZP6E4dmCNXmcoWHDFus+o3uJNlKyBsBkpmS19HfX ajwiE+vOlpPyEnBTMKmKvVrl/A1wx2dth3ecKA5s70CKH8xi2kWvFHXi8ykEA91dfNLn srSCnY4zFrelhimmZOiNNT2w2o4UGT+UGzoF9TOhjirdjxaiU+i4o7pQ5CHq/i7Qusj3 N+1CAZ4BlGugN/519MxSlrw8xaUNP+czpyvVaf9Ijsac+q0Z6vKKtkgJzCvTBLGwKzga mtOA==
MIME-Version 1.0
X-Received by 10.58.255.199 with SMTP id as7mr698509ved.23.1370663941143; Fri, 07 Jun 2013 20:59:01 -0700 (PDT)
In-Reply-To <20130607213239.3e39a448@bigbox.christie.dr>
References <20130607213239.3e39a448@bigbox.christie.dr>
Date Sat, 8 Jun 2013 13:59:01 +1000
Subject Re: Idiomatic Python for incrementing pairs
From Chris Angelico <rosuav@gmail.com>
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.2872.1370663943.3114.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370663943 news.xs4all.nl 15911 [2001:888:2000:d::a6]:53862
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:47361

Show key headers only | View raw


On Sat, Jun 8, 2013 at 12:32 PM, Tim Chase
<python.list@tim.thechases.com> wrote:
>   def calculate(params):
>     a = b = 0
>     if some_calculation(params):
>       a += 1
>     if other_calculation(params):
>       b += 1
>     return (a, b)
>
>   alpha = beta = 0
>   temp_a, temp_b = calculate(...)
>   alpha += temp_a
>   beta += temp_b
>
> Is there a better way to do this without holding each temporary
> result before using it to increment?

Can you pass the function a list with the appropriate values in them,
and have it return them via that?

  def calculate(params, sums):
    if some_calculation(params):
      sums[0] += 1
    if other_calculation(params):
      sums[1] += 1

  sums = [0, 0]
  calculate(..., sums)

Or use a dictionary if you'd rather they have names, either way.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Idiomatic Python for incrementing pairs Chris Angelico <rosuav@gmail.com> - 2013-06-08 13:59 +1000

csiph-web