[Tutor] doing maths on lists

Peter Otten __peter__ at web.de
Mon Dec 20 17:47:49 CET 2010


Dave Angel wrote:

> from math import cos
> 
> JME = 0.4
> L0A = [2.3, 4.65]
> L0B = [1.8, 2.2]
> L0C = [12.1, 4]
> limit = len(L0A)
> 
> L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit))

OP, if you are planning to do this "maths on lists" a lot you should have a 
look at numpy. With numpy Dave's snippet can be written as

from numpy import array, cos, sum

j = 0.4
a = array([2.3, 4.65])
b = array([1.8, 2.2])
c = array([12.1, 4])

print sum(a*cos(b+c*j))

Peter



More information about the Tutor mailing list