[Tutor] whats the best way to structure my data?

Jeff Peery jeffpeery at yahoo.com
Fri May 11 02:17:26 CEST 2007


ok, thanks. so is there a difference in performance if I do it this way versus if I use say a numpy function on an array? thanks.
 
 Jeff

John Fouhy <john at fouhy.net> wrote: On 11/05/07, Jeff Peery  wrote:
> hello, I was wondering what might be the best way to structure my data
> within python. I am sampling data points, and in each there is a time,
> value, and text string associated with that sample. so I was thinking I'd
> make a list of 'measurement objects' and each object would have the
> attributes: time, value, and text... but I want to operate on the numerical
> values to find the average and stdev... so I'm not sure how to operate on my
> data if it is inside an object.
[...]
> mean = numpy.mean(????)
>
> is there a way to operate on the data when it is structured like this?

You could use a list comprehension or a generator expression.

eg:

mean = sum(m.value for m in measurements)/len(measurements)

Also, here is an alternative way you could store your data: as a list
of tuples.  You could write code like:

# measurements is a list of tuples.  Each element is a triple (value,
text, time).
measurements = []

Then, later on, when you get a new measurement, something like:

measurements.append((value, text, time))

You can find the mean in the same way:

mean = sum(m[0] for m in measurements)/len(measurement)

-- 
John.


 	      
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070510/26103d07/attachment.html 


More information about the Tutor mailing list