Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35145 > unrolled thread
| Started by | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| First post | 2012-12-19 12:24 -0600 |
| Last post | 2012-12-19 12:24 -0600 |
| 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.
Re: calculation on lists Tim Chase <python.list@tim.thechases.com> - 2012-12-19 12:24 -0600
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-12-19 12:24 -0600 |
| Subject | Re: calculation on lists |
| Message-ID | <mailman.1066.1355941422.29569.python-list@python.org> |
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
Back to top | Article view | comp.lang.python
csiph-web