Re: consistency: extending arrays vs. multiplication ?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: consistency: extending arrays vs. multiplication ?

From: Soeren Sonnenburg <python-ml@nn7.de>
Date: Sun Jul 24 2005 - 16:30:19 CEST

On Sat, 2005-07-23 at 20:25 -0700, Dan Bishop wrote:
> Soeren Sonnenburg wrote:
> > Hi all,
> >
> > Just having started with python, I feel that simple array operations '*'
> > and '+' don't do multiplication/addition but instead extend/join an
> > array:
> >
> > a=[1,2,3]
> > >>> b=[4,5,6]
> > >>> a+b
> > [1, 2, 3, 4, 5, 6]
> >
> > instead of what I would have expected:
> > [5,7,9]
>
> To get what you expected, use
>
> [x + y for (x, y) in zip(a, b)]

Thanks for this suggestion, however I am interested in understanding the
design decision here... I could aswell just use numarray and get the
wanted a+b by:

from numarray import *
a=array([1,2,3])
b=array([1,2,3])
a+b
array([2, 4, 6])

Soeren
Received on Thu Sep 29 17:08:27 2005