[Numpy-discussion] which one is best?
mark
markbak@gmail....
Fri Sep 19 07:40:35 CDT 2008
I need to multiply items in a list and need a list back. Which one of
the four options is best (I thought in Python there was only one way
to do something???)
>>> a = [1,2,3,4]
>>> b = [10,20,30,40]
>>> c = [] # method 1
>>> for i in range(len(a)):
c.append(a[i]*b[i])
>>> c
[10, 40, 90, 160]
>>> array(a)*array(b) # method 2, but need to convert back to list
array([ 10, 40, 90, 160])
>>> [ a[i] * b[i] for i in range(len(a)) ] # method 3
[10, 40, 90, 160]
>>> [ x * y for x,y in zip(a,b) ] # method 4
[10, 40, 90, 160]
Thanks for any insights, Mark
More information about the Numpy-discussion
mailing list