Velocity Reviews > calculation on lists

# calculation on lists

loïc Lauréote
Guest
Posts: n/a

 12-19-2012

I found something allowing to avoid loops.

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: http://www.velocityreviews.com/forums/(E-Mail Removed)
> To: (E-Mail Removed)
> CC: (E-Mail Removed)
>
> 2012/12/19 loïc Lauréote <(E-Mail Removed)>:
> 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Tim Chase Python 0 12-19-2012 06:24 PM Vlastimil Brom Python 0 12-19-2012 05:24 PM Vlastimil Brom Python 0 12-19-2012 12:38 PM loïc Lauréote Python 0 12-19-2012 12:25 PM =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM