Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'operator': 0.03; 'heavily': 0.04; 'class,': 0.07; 'thx': 0.09; 'cc:addr:python-list': 0.10; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'message- id:@tim.thechases.com': 0.16; 'numpy': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'vectors': 0.16; 'wrote:': 0.17; 'duplicate': 0.17; '>>>': 0.18; 'code,': 0.18; 'written': 0.20; 'simpler': 0.22; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'core': 0.27; '(such': 0.27; 'installed,': 0.29; 'probably': 0.29; "i'm": 0.29; 'function': 0.30; 'subject:lists': 0.32; 'print': 0.32; "aren't": 0.33; 'cases,': 0.33; 'interface,': 0.33; 'handle': 0.33; 'hi,': 0.33; 'version': 0.34; 'list': 0.35; 'faster': 0.35; 'something': 0.35; 'there': 0.35; 'tool': 0.36; 'but': 0.36; 'stock': 0.36; 'should': 0.36; 'thank': 0.36; 'does': 0.37; 'being': 0.37; 'skip:z 10': 0.37; 'rather': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'your': 0.60; 'more': 0.63; 'want,': 0.65; 'answer.': 0.71; 'obvious': 0.71; 'to:charset:iso-8859-1': 0.75; 'issues:': 0.84; 'received:50.22': 0.84; 'yours': 0.88; '======': 0.91; 'canonical': 0.91 Date: Wed, 19 Dec 2012 12:24:40 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: =?ISO-8859-1?Q?lo=EFc_Laur=E9ote?= Subject: Re: calculation on lists References: , In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: Python 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355941422 news.xs4all.nl 6921 [2001:888:2000:d::a6]:50861 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35145 On 12/19/12 09:24, loïc Lauréote wrote: >> is there a tool to calculate on list ? >> >> something like : >> >>> a= [1,1,1,1] >>> b = [5,9,8,4] >>> c = a+b*a >>> print c >>> [6,10,9,5] >> >> Thx >> >> ====== >> >> Hi, >> for such simpler cases, you may try list comprehensions and probably >> the zip(...) function >> >>>>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] >> [6, 10, 9, 5] > Thank for your answer, > > I found something allowing to avoid loops. I use operator > overloading. This is a pretty obvious case of "don't duplicate others' effort". With stock python, Vlastimil's list-comprehension using zip() is the canonical answer. I'm pretty sure it's far faster than your class, in addition to feeling far more pythonic. If you happen to have numpy installed, Vlastimil's answer there already does what you want, but has the added benefits of 1) being heavily tested 2) having core parts written in C for efficiency 3) having a complete intuitive interface, rather than yours which may have issues (such as not having left-vs-right issues: a * 5 5 * a should produce the same results) 4) both the list-comprehension version and the numpy version aren't limited to 2-element vectors like your code, but can handle N-element vectors. -tkc