Get all items in the list where the value is equal to a specific value

I have a list that 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 summarize (actually take the average ... but this is a detail) all the values ​​of the strings together, where the value of the first element is equal. This would mean that in the above example, the first two lines would be added together.

 [[3, 8.5675, 13.0665], [2, 3.986666666666667, 6.6433333333333335], [1, 3.9542857142857137, 5.674285714285714],....] 

This means that the first values ​​must be unique.

I thought about this, finding all the "lines" where the first value is equal to, for example, 1 and sums them together. Now my question is: how can I find all rows where the first value is equal to a specific value.

+4
source share
3 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 little 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:]) 

Conclusion, 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 it instead of the sum function in map :

 mean = lambda x: sum(x) / float(len(x)) map(mean, zip...) 
+2
source

In Python, there are many ways to do something like this. If your list is called a , you can compose a list view to get row indices where the first column is 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 find all kinds of statistics. The large number of libraries available is one of the many things that make developing with Python such a fantastic experience.

+2
source
 >>> from itertools import groupby >>> alist [[3, 4.6575, 7.3725], [3, 3.91, 5.694], [2, 3.986666666666667, 6.6433333333333335], [1, 3.9542857142857137, 5.674285714285714]] >>> [reduce(lambda x, y: [key, x[1]+y[1], x[2]+y[2]], group) for key, group in groupby(alist, lambda x:x[0])] [[3, 8.567499999999999, 13.0665], [2, 3.986666666666667, 6.6433333333333335], [1, 3.9542857142857137, 5.674285714285714]] 

I just propose another solution using list comprehension, groupby and reduce . reduce should be imported from functools in py3.x.

+1
source

Source: https://habr.com/ru/post/1492247/


All Articles