numarray :: multiplying all the elements in 1d array

Mandus mandus at gmail.com
Tue Dec 20 09:46:36 EST 2005


Tue, 20 Dec 2005 19:32:13 +0530 skrev Suresh Jeevanandam:
> Hi all,
> 	Lets say I have an array:
> 	from numarray import *
> 	a = array([ 6,  7,  8,  9, 10, 11, 12])
>
> 	I want to multiply out all the elements and get the result.
> 	
> 	r = 1.0
> 	for i in a:
>   		r = r*i
>
> 	Is there any faster, efficient way of doing this.

You can use multiply.reduce(a) (multiply is a function imported from
numarray).

With regular python you can also do:

from operator import mul
reduce(mul,a)

This work even when 'a' is a plain python list.


-- 
Mandus - the only mandus around.



More information about the Python-list mailing list