Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47392
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'assignment': 0.07; 'element': 0.07; 'variables': 0.07; 'arrays': 0.09; 'augmented': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'val': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '12]': 0.16; 'alpha,': 0.16; 'b):': 0.16; 'cleaner': 0.16; 'in-place': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'temp': 0.16; 'unpacking': 0.16; 'val,': 0.16; 'written.': 0.16; 'alpha': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'version.': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; "haven't": 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'tim': 0.29; 'absolute': 0.30; 'said,': 0.30; 'sets': 0.30; 'code': 0.31; 'chase': 0.31; 'question:': 0.31; 'class': 0.32; 'probably': 0.32; 'bugs': 0.33; 'checking': 0.33; 'actual': 0.34; 'skip:_ 10': 0.34; 'maybe': 0.34; 'problem': 0.35; 'but': 0.35; 'version': 0.36; 'two': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'to:addr:python- list': 0.38; 'rather': 0.38; 'little': 0.38; 'anything': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'mentioned': 0.61; 'received:173': 0.61; 'here': 0.66; 'reply': 0.66; '(here': 0.84; 'received:fios.verizon.net': 0.84; 'carlos': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Jan Reedy <tjreedy@udel.edu> |
| Subject | Re: Idiomatic Python for incrementing pairs |
| Date | Sat, 08 Jun 2013 13:29:21 -0400 |
| References | <20130607213239.3e39a448@bigbox.christie.dr> <BLU176-W41787828AF1636314057B8D79A0@phx.gbl> <20130607231622.31cb120b@bigbox.christie.dr> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 |
| In-Reply-To | <20130607231622.31cb120b@bigbox.christie.dr> |
| 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.2889.1370712552.3114.python-list@python.org> (permalink) |
| Lines | 63 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1370712552 news.xs4all.nl 16006 [2001:888:2000:d::a6]:52795 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:47392 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idiomatic Python for incrementing pairs Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-08 13:29 -0400
csiph-web