I have a list which looks like this:
[[3, 4.6575, 7.3725],
[3, 3.91, 5.694],
[2, 3.986666666666667, 6.6433333333333335],
[1, 3.9542857142857137, 5.674285714285714],....]
I would like to sum (in fact take the mean ... but it is a detail) all the values of the rows together where the value of the first element are equal. This would mean that in the example above the first two rows would be summed together.
[[3, 8.5675, 13.0665],
[2, 3.986666666666667, 6.6433333333333335],
[1, 3.9542857142857137, 5.674285714285714],....]
This means the first values should be unique.
I thought of doing this by finding all the "rows" where the first value is equal to for example to 1 and sum them together. My question is now, how can I find all the rows where the first value is equal to a certain value.
This should work:
lst = [[3, 4.6575, 7.3725],
[3, 3.91, 5.694],
[2, 3.986666666666667, 6.6433333333333335],
[1, 3.9542857142857137, 5.674285714285714]]
# group the values in a dictionary
import collections
d = collections.defaultdict(list)
for item in lst:
d[item[0]].append(item)
# find sum of values
for key, value in d.items():
print [key] + map(sum, zip(*value)[1:])
Or, a bit cleaner, using itertools.groupby
:
import itertools
groups = itertools.groupby(lst, lambda i: i[0])
for key, value in groups:
print [key] + map(sum, zip(*value)[1:])
Output, in both cases:
[1, 3.9542857142857137, 5.674285714285714]
[2, 3.986666666666667, 6.6433333333333335]
[3, 8.567499999999999, 13.0665]
If you want to calculate the mean instead of the sum, just define your own mean
function and pass that one instead of the sum
function to map
:
mean = lambda x: sum(x) / float(len(x))
map(mean, zip...)
There are many ways to do something like this in Python.
If your list is called a
, you can make a list comprehension to get the row indices where first column is equal to value
:
rows = [i for i in range(0,len(a)) if a[i][0]==value]
However, I'm sure there are whole libraries that parse arrays or lists in X dimensions to retreive all kinds of statistical data out there. The high number of libraries available is one of the many thing that make developing with Python such a fantastic experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With