Sum / Average an attribute of a list of objects
If you are looking for other measures than sum, e.g. mean/standard deviation, you can use NumPy and do:
mean = np.mean([c.a for c in c_list])
sd = np.std([c.a for c in c_list])
Use a generator expression:
sum(c.a for c in c_list)
I had a similar task, but mine involved summing a time duration as your attribute c.a
.
Combining this with another question asked here, I came up with
sum((c.a for c in cList), timedelta())
Because, as mentioned in the link, sum
needs a starting value.