list*list

B

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*b)
a = c
print a
[1, 4, 9]

Perhaps a list comprehension or is this better addressed by NumPy?

Thanks,

jab
 
D

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*b)
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 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.

You can use a listcomp like this:

c = [a * b for i in xrange(len(a))]

But maybe nicer is zip:

c = [av * bv for av, bv in zip(a, b)]

And if lists get large and perhaps multidemnsional, numpy certainly is the
way to go.

Diez
 
T

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*b)
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 for q,r in zip(a,b)]

seems to do the trick for me.

-tim
 
D

David Isaac

Diez B. Roggisch said:
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.
Cheers,
Alan Isaac
 
F

Fredrik Lundh

David said:
I'm pretty sure this distinction goes away in 2.5.

3.0.

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.

(it's not always the most efficient thing to do, though, but that's
another story).

</F>
 
D

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 memory consumption of
his little script that contained a range(9000000) which he used for a loop.

And as in the case of a "for in" one can't even access the list produced by
range() and thus there is virtually no difference to xrange, I'd consider
it a code smell if it's used there - why creating an object you don't use
when you can't even get a reference to it?

Diez
 
K

Klaas

Diez said:
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 be "bad style" to create an
unnecessary list with zip instead of using izip?

-Mike
 
D

Diez B. Roggisch

Klaas said:
Diez said:
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 be "bad style" to create an
unnecessary list with zip instead of using izip?

Yep. I always forget about the itertools. And then of course we'd go for
an generator expression, won't we?


Diez
 
Z

Ziga Seilnacht

BBands said:
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 purpose:
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([1, 2, 3])
c = a * b
c
array([1, 4, 9])

Otherwise, you can use the builtin function map and functions in the
operator module:
import operator
a = [1, 2, 3]
b = [1, 2, 3]
c = map(operator.mul, a, b)
c [1, 4, 9]
d = map(operator.add, a, b)
d
[2, 4, 6]
Thanks,

jab

Ziga
 
B

BBands

Very useful comments... Thanks to all!

Once again this community has demonstrated why Python is THE language.

jab
 
B

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]
[arow * brow for arow, brow in izip(a, b)]
[4, 10, 18]

Bye,
bearophile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,203
Latest member
LillianaFr

Latest Threads

Top