Re: [Numpy-discussion] which one is best?
2008/9/19 mark [EMAIL PROTECTED]: 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???) With the emphasis on preferably and obvious :) There should be one-- and preferably only one --obvious way to do it. The modern idiom is the list comprehension, rather than the for-loop. Of those options, I personally prefer using zip. [ x * y for x,y in zip(a,b) ] # method 4 [10, 40, 90, 160] If you have very large arrays, you can also consider (np.array(x) * np.array(y)).tolist() Cheers Stéfan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] which one is best?
On Fri, Sep 19, 2008 at 3:09 PM, Stéfan van der Walt [EMAIL PROTECTED]wrote: 2008/9/19 mark [EMAIL PROTECTED]: 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???) With the emphasis on preferably and obvious :) There should be one-- and preferably only one --obvious way to do it. The modern idiom is the list comprehension, rather than the for-loop. Of those options, I personally prefer using zip. [ x * y for x,y in zip(a,b) ] # method 4 [10, 40, 90, 160] If you have very large arrays, you can also consider (np.array(x) * np.array(y)).tolist() Cheers Stéfan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion I think [x*y for x in a for y in b] feels pythonic, however it has a surprisingly lousy performance. In [30]: %timeit [ x * y for x,y in zip(a,b) ] 10 loops, best of 3: 3.96 µs per loop In [31]: %timeit [ i*j for i in a for j in b ] 10 loops, best of 3: 6.53 µs per loop In [32]: a = range(100) In [33]: b = range(100) In [34]: %timeit [ x * y for x,y in zip(a,b) ] 1 loops, best of 3: 51.9 µs per loop In [35]: %timeit [ i*j for i in a for j in b ] 100 loops, best of 3: 2.78 ms per loop Arnar ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] which one is best?
On Fri, Sep 19, 2008 at 4:09 PM, lorenzo [EMAIL PROTECTED] wrote: On Fri, Sep 19, 2008 at 2:50 PM, Arnar Flatberg [EMAIL PROTECTED]wrote: I think [x*y for x in a for y in b] feels pythonic, however it has a surprisingly lousy performance. This returns a len(x)*len(y) long list, which is not what you want. My bad, Its friday afternoon, I'll go home now :-) Arnar ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] which one is best?
Hi Arnar, Your two commands below aren't doing the same thing - one is doing a[i]*b[i] and the other is doing a[i]*b[j] for all i and j. As the second is harder, it takes longer. Cheers, David On Fri, 2008-09-19 at 09:08 -0500, [EMAIL PROTECTED] wrote: I think [x*y for x in a for y in b] feels pythonic, however it has a surprisingly lousy performance. In [30]: %timeit [ x * y for x,y in zip(a,b) ] 10 loops, best of 3: 3.96 ?s per loop In [31]: %timeit [ i*j for i in a for j in b ] 10 loops, best of 3: 6.53 ?s per loop In [32]: a = range(100) In [33]: b = range(100) In [34]: %timeit [ x * y for x,y in zip(a,b) ] 1 loops, best of 3: 51.9 ?s per loop In [35]: %timeit [ i*j for i in a for j in b ] 100 loops, best of 3: 2.78 ms per loop Arnar -- ** David M. Kaplan Charge de Recherche 1 Institut de Recherche pour le Developpement Centre de Recherche Halieutique Mediterraneenne et Tropicale av. Jean Monnet B.P. 171 34203 Sete cedex France Phone: +33 (0)4 99 57 32 27 Fax: +33 (0)4 99 57 32 95 http://www.ur097.ird.fr/team/dkaplan/index.html ** ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion