Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35132 > unrolled thread
| Started by | loïc Lauréote <laureote-loic@hotmail.fr> |
|---|---|
| First post | 2012-12-19 16:24 +0100 |
| Last post | 2012-12-19 16:24 +0100 |
| 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.
calculation on lists loïc Lauréote <laureote-loic@hotmail.fr> - 2012-12-19 16:24 +0100
| From | loïc Lauréote <laureote-loic@hotmail.fr> |
|---|---|
| Date | 2012-12-19 16:24 +0100 |
| Subject | calculation on lists |
| Message-ID | <mailman.1055.1355930719.29569.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
Thank for your answer,
I found something allowing to avoid loops.
I use operator overloading.
import math
class Vector:
def __init__(self, x=0, y=0):
self.x=x
self.y=y
def __eq__(self, vB): return (self.x==vB.x) and (self.y==vB.y)
def __add__(self, vB): return Vector(self.x+vB.x,self.y+vB.y)
def __sub__(self, vB): return Vector(self.x-vB.x,self.y-vB.y)
def __mul__(self, c):
if isinstance(c,Vector): return Vector(self.x*c.x,self.y*c.y)
else: return Vector(c*self.x,c*self.y)
def __div__(self, c):
if isinstance(c,Vector): return Vector(self.x/c.x,self.y/c.y)
else: return Vector(c*self.x,c*self.y)
a = Vector(4,5)
b = Vector(6,7)
print a,b
print b*b+a
thx
> Date: Wed, 19 Dec 2012 13:38:28 +0100
> Subject: Re: calculation on lists
> From: vlastimil.brom@gmail.com
> To: laureote-loic@hotmail.fr
> CC: python-list@python.org
>
> 2012/12/19 loïc Lauréote <laureote-loic@hotmail.fr>:
> hi,
> I
> have a question,
> 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]
> >>>
>
> hth,
> vbr
Back to top | Article view | comp.lang.python
csiph-web