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

John Fouhy john at fouhy.net
Fri May 11 01:38:28 CEST 2007

On 11/05/07, Jeff Peery <jeffpeery at yahoo.com> 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.


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)


More information about the Tutor mailing list