Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'thx': 0.09; 'def': 0.10; 'dec': 0.15; 'c):': 0.16; 'received:65.55.116.7': 0.16; 'wed,': 0.16; '>>>': 0.18; 'math': 0.20; 'skip:v 30': 0.20; 'to:name:python-list@python.org': 0.20; 'import': 0.21; '>>>': 0.22; 'simpler': 0.22; '>': 0.23; 'header:In-Reply-To:1': 0.25; '+0100': 0.27; 'date:': 0.29; 'skip:_ 10': 0.29; 'skip:& 10': 0.29; 'probably': 0.29; 'class': 0.29; 'function': 0.30; 'lists': 0.31; 'subject:lists': 0.32; 'print': 0.32; 'cases,': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'list': 0.35; 'something': 0.35; 'there': 0.35; 'subject:': 0.36; 'tool': 0.36; 'email addr:python.org': 0.36; 'thank': 0.36; 'skip:v 20': 0.37; 'skip:z 10': 0.37; 'from:': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'email name:python- list': 0.62; 'email addr:gmail.com': 0.63; 'from:addr:hotmail.fr': 0.84; '======': 0.91 X-EIP: [OKSWP7dPhr1dz2fB9kVieuaD06V/xVU/] X-Originating-Email: [laureote-loic@hotmail.fr] Content-Type: multipart/alternative; boundary="_efa5073d-c2a1-4f46-92f3-dc958dfcfa3d_" From: =?iso-8859-1?B?bG/vYyBMYXVy6W90ZQ==?= To: "python-list@python.org" Subject: calculation on lists Date: Wed, 19 Dec 2012 16:24:11 +0100 Importance: Normal In-Reply-To: References: , MIME-Version: 1.0 X-OriginalArrivalTime: 19 Dec 2012 15:24:11.0335 (UTC) FILETIME=[E55F7D70:01CDDDFC] 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: 134 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355930719 news.xs4all.nl 6972 [2001:888:2000:d::a6]:44331 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35132 --_efa5073d-c2a1-4f46-92f3-dc958dfcfa3d_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Thank for your answer=2C I found something allowing to avoid loops. I use operator overloading. import math class Vector: def __init__(self=2C x=3D0=2C y=3D0): self.x=3Dx self.y=3Dy def __eq__(self=2C vB): return (self.x=3D=3DvB.x) and (self.y=3D=3DvB.y= ) =20 def __add__(self=2C vB): return Vector(self.x+vB.x=2Cself.y+vB.y) def __sub__(self=2C vB): return Vector(self.x-vB.x=2Cself.y-vB.y) def __mul__(self=2C c): if isinstance(c=2CVector): return Vector(self.x*c.x=2Cself.y*c.y) else: return Vector(c*self.x=2Cc*self.y) =20 def __div__(self=2C c): if isinstance(c=2CVector): return Vector(self.x/c.x=2Cself.y/c.y) else: return Vector(c*self.x=2Cc*self.y) =20 a =3D Vector(4=2C5) b =3D Vector(6=2C7) print a=2Cb print b*b+a thx > Date: Wed=2C 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 >=20 > 2012/12/19 lo=EFc Laur=E9ote : > hi=2C > I > have a question=2C > is there a tool to calculate on list ? >=20 > something like : >=20 > >a=3D [1=2C1=2C1=2C1] > >b =3D [5=2C9=2C8=2C4] > >c =3D a+b*a > >print c > >[6=2C10=2C9=2C5] >=20 > Thx >=20 > =3D=3D=3D=3D=3D=3D >=20 > Hi=2C > for such simpler cases=2C you may try list comprehensions and probably > the zip(...) function >=20 > >>> [a+b*a for a=2Cb in zip([1=2C1=2C1=2C1]=2C [5=2C9=2C8=2C4])] > [6=2C 10=2C 9=2C 5] > >>> >=20 > hth=2C > vbr = --_efa5073d-c2a1-4f46-92f3-dc958dfcfa3d_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Thank for your answer=2C

I found something allowing to avoid loops.<= br>I use operator overloading.


import math

class Vector:<= br> =3B =3B =3B def __init__(self=2C x=3D0=2C y=3D0):
 = =3B =3B =3B =3B =3B =3B =3B self.x=3Dx
 =3B&= nbsp=3B =3B =3B =3B =3B =3B self.y=3Dy
 =3B = =3B =3B def __eq__(self=2C vB): return (self.x=3D=3DvB.x) and (self.y= =3D=3DvB.y) =3B =3B =3B =3B =3B =3B =3B =3B=
 =3B =3B =3B def __add__(self=2C vB): =3B return Vecto= r(self.x+vB.x=2Cself.y+vB.y)
 =3B =3B =3B def __sub__(self= =2C vB): =3B return Vector(self.x-vB.x=2Cself.y-vB.y)
 =3B = =3B =3B def __mul__(self=2C c):
 =3B =3B =3B =3B&nbs= p=3B =3B =3B if isinstance(c=2CVector): return =3B Vector(self.= x*c.x=2Cself.y*c.y)
 =3B =3B =3B =3B =3B =3B&nbs= p=3B else: return Vector(c*self.x=2Cc*self.y)

 =3B =3B = =3B
 =3B =3B =3B def __div__(self=2C c):
 =3B = =3B =3B =3B =3B =3B =3B if isinstance(c=2CVector): retu= rn =3B Vector(self.x/c.x=2Cself.y/c.y)
 =3B =3B =3B = =3B =3B =3B =3B else: return Vector(c*self.x=2Cc*self.y)
&nb= sp=3B =3B =3B


a =3D Vector(4=2C5)
b =3D Vector(6=2C7= )
print a=2Cb
print b*b+a


thx


>=3B Date: Wed=2C 19 Dec 2012 13:38:28 +0100>=3B Subject: Re: calculation on lists
>=3B From: vlastimil.brom@gm= ail.com
>=3B To: laureote-loic@hotmail.fr
>=3B CC: python-list@py= thon.org
>=3B
>=3B 2012/12/19 lo=EFc Laur=E9ote <=3Blaureote-l= oic@hotmail.fr>=3B:
>=3B hi=2C
>=3B I
>=3B have a questio= n=2C
>=3B is there a tool to calculate on list ?
>=3B
>=3B = something like :
>=3B
>=3B >=3Ba=3D [1=2C1=2C1=2C1]
>=3B = >=3Bb =3D [5=2C9=2C8=2C4]
>=3B >=3Bc =3D a+b*a
>=3B >=3Bpri= nt c
>=3B >=3B[6=2C10=2C9=2C5]
>=3B
>=3B Thx
>=3B >=3B =3D=3D=3D=3D=3D=3D
>=3B
>=3B Hi=2C
>=3B for such s= impler cases=2C you may try list comprehensions and probably
>=3B the = zip(...) function
>=3B
>=3B >=3B>=3B>=3B [a+b*a for a=2Cb = in zip([1=2C1=2C1=2C1]=2C [5=2C9=2C8=2C4])]
>=3B [6=2C 10=2C 9=2C 5]>=3B >=3B>=3B>=3B
>=3B
>=3B hth=2C
>=3B vbr
=
= --_efa5073d-c2a1-4f46-92f3-dc958dfcfa3d_--