Re: list*list

2006-05-03 Thread BBands
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

Re: list*list

2006-05-03 Thread bearophileHUGS
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) [4, 10, 18]

list*list

2006-05-01 Thread BBands
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 --

Re: list*list

2006-05-01 Thread Tim Williams
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] :) --

Re: list*list

2006-05-01 Thread Diez B. Roggisch
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 considered bad style

Re: list*list

2006-05-01 Thread Tim Chase
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] c = [q*r

Re: list*list

2006-05-01 Thread David Isaac
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 in

Re: list*list

2006-05-01 Thread Fredrik Lundh
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't

Re: list*list

2006-05-01 Thread Diez B. Roggisch
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 the

Re: list*list

2006-05-01 Thread Klaas
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 also

Re: list*list

2006-05-01 Thread Diez B. Roggisch
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)] By your

Re: list*list

2006-05-01 Thread Ziga Seilnacht
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