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


Groups > comp.lang.python > #47392 > unrolled thread

Re: Idiomatic Python for incrementing pairs

Started byTerry Jan Reedy <tjreedy@udel.edu>
First post2013-06-08 13:29 -0400
Last post2013-06-08 13:29 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Idiomatic Python for incrementing pairs Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-08 13:29 -0400

#47392 — Re: Idiomatic Python for incrementing pairs

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-06-08 13:29 -0400
SubjectRe: Idiomatic Python for incrementing pairs
Message-ID<mailman.2889.1370712552.3114.python-list@python.org>
On 6/8/2013 12:16 AM, Tim Chase wrote:
> On 2013-06-08 07:04, Carlos Nepomuceno wrote:
>> alpha, beta = (1 if some_calculation(params) else 0, 1 if
>> other_calculation(params) else 0)
>
> This one sets them to absolute values, rather than the incrementing
> functionality in question:
>
>>>    alpha += temp_a
>>>    beta += temp_b
>
> The actual code in question does the initialization outside a loop:
>
>    alphas_updated = betas_updated = 0
>    for thing in bunch_of_things:
>      a, b = process(thing)
>      alphas_updated += a
>      betas_updated += b

I am pretty sure that this is the faster way to get the result.

> and it just bugs me as being a little warty for having temp
> variables when Python does things like tuple-unpacking so elegantly.

So use it ;-).

> That said, as mentioned in a contemporaneous reply to Jason, I haven't
> found anything better that is still readable.

The generalization of what you want is a mutable vector with an in-place 
augmented assignment version of element by element addition. That 
probably has been written. Nnmpy arrays do vector addition and maybe do 
it in-place also (I just do ot know). But here is a custom class for the 
problem you presented.

def process(val):
     return val, 2*val

class Pair(list):
     def __init__(self, a, b):
         self.append(a)
         self.append(b)
     def inc(self, a, b):
         self[0] += a
         self[1] += b

pair = Pair(0, 0)
for val in [1,2,3]:
     pair.inc(*process(val))

print(pair)
 >>>
[6, 12]

This is cleaner but a bit slower than your in-lined version. I did not 
use __iadd__ and += because unpacking 'other' (here the process return) 
in the call does the error checking ('exactly two values') for 'free'.

--
Terry Jan Reedy


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web