Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35132

calculation on lists

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 <laureote-loic@hotmail.fr>
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; '&gt;&gt;&gt;': 0.22; 'simpler': 0.22; '&gt;': 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 loïc Lauréote <laureote-loic@hotmail.fr>
To "python-list@python.org" <python-list@python.org>
Subject calculation on lists
Date Wed, 19 Dec 2012 16:24:11 +0100
Importance Normal
In-Reply-To <CAHzaPEPXMLSqhqyzSj96xFcedjr=HeoPv+d_xGrOwXAqLe-PFQ@mail.gmail.com>
References <CAHzaPEPXMLSqhqyzSj96xFcedjr=HeoPv+d_xGrOwXAqLe-PFQ@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1055.1355930719.29569.python-list@python.org> (permalink)
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

Show key headers only | View raw


[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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

calculation on lists loïc Lauréote <laureote-loic@hotmail.fr> - 2012-12-19 16:24 +0100

csiph-web