Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all elements in a list where the value is equal to certain value

Tags:

python

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.

like image 407
Olivier_s_j Avatar asked Jul 19 '13 08:07

Olivier_s_j


2 Answers

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...)
like image 117
tobias_k Avatar answered Oct 11 '22 22:10

tobias_k


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.

like image 24
Henrik Avatar answered Oct 12 '22 00:10

Henrik