Ziga Seilnach:
>c = map(operator.mul, a, b)
Usually I like map a lot, but this time for me the l.c. version is a
bit simpler to understand (even if it's longer, and maybe slower too):
>>> from operator import mul
>>> from itertools import izip
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> map(mul, a, b
Very useful comments... Thanks to all!
Once again this community has demonstrated why Python is THE language.
jab
--
http://mail.python.org/mailman/listinfo/python-list
BBands wrote:
> There must be a better way to multiply the elements of one list by
> another:
[snipped]
> Perhaps a list comprehension or is this better addressed by NumPy?
If you have a large amount of numerical code, it is definetly better to
use numpy, since it is intended just for that purpo
Klaas schrieb:
> Diez wrote:
>> First of all: it's considered bad style to use range if all you want is a
>> enumeration of indices, as it will actually create a list of the size you
>> specified. Use xrange in such cases.
>
>> But maybe nicer is zip:
>> c = [av * bv for av, bv in zip(a, b)]
>
>
Diez wrote:
> First of all: it's considered bad style to use range if all you want is a
> enumeration of indices, as it will actually create a list of the size you
> specified. Use xrange in such cases.
> But maybe nicer is zip:
> c = [av * bv for av, bv in zip(a, b)]
By your logic, shouldn't it
> and for short sequences, it doesn't really matter much in the 2.X series.
> it's definitely not "bad style" to use range instead of xrange for a ten
> to, say, 1000 item loop.
You and me are aware of that - but I figured the OP wasn't. And just the
other day somebody on de.c.l.py wondered about
David Isaac wrote:
> > it's considered bad style to use range if all you want is a
> > enumeration of indices, as it will actually create a list of the size you
> > specified. Use xrange in such cases.
>
> I'm pretty sure this distinction goes away in 2.5.
3.0.
and for short sequences, it doesn'
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> it's considered bad style to use range if all you want is a
> enumeration of indices, as it will actually create a list of the size you
> specified. Use xrange in such cases.
I'm pretty sure this distinction goes away
> There must be a better way to multiply the elements of one list by
> another:
>
> a = [1,2,3]
> b = [1,2,3]
> c = []
> for i in range(len(a)):
> c.append(a[i]*b[i])
> a = c
> print a
> [1, 4, 9]
>
> Perhaps a list comprehension or is this better addressed by NumPy?
a = [1,2,3]
b = [1,2,3
> There must be a better way to multiply the elements of one list by
> another:
>
> a = [1,2,3]
> b = [1,2,3]
> c = []
> for i in range(len(a)):
> c.append(a[i]*b[i])
> a = c
> print a
> [1, 4, 9]
>
> Perhaps a list comprehension or is this better addressed by NumPy?
First of all: it's consider
On 1 May 2006 08:28:12 -0700, BBands <[EMAIL PROTECTED]> wrote:
There must be a better way to multiply the elements of one list byanother:a = [1,2,3]b = [1,2,3]c = []for i in range(len(a)):c.append(a[i]*b[i])a = cprint a[1, 4, 9]
Something like:
>>> [ x * y for x,y in zip(a,b) ]
[1, 4, 9
There must be a better way to multiply the elements of one list by
another:
a = [1,2,3]
b = [1,2,3]
c = []
for i in range(len(a)):
c.append(a[i]*b[i])
a = c
print a
[1, 4, 9]
Perhaps a list comprehension or is this better addressed by NumPy?
Thanks,
jab
--
http://mail.python.org/
12 matches
Mail list logo