简体   繁体   English

获取列表中的所有元素,其中值等于特定值

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

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. 我想通过找到第一个值等于例如1的所有“行”并将它们加在一起来做到这一点。 My question is now, how can I find all the rows where the first value is equal to a certain value. 我现在的问题是,如何找到第一个值等于某个值的所有行。

There are many ways to do something like this in Python. 在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 : 如果您的列表名为a ,则可以进行列表推导以获取第一列等于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. 但是,我确信有完整的库可以解析X维中的数组或列表,以便在那里检索各种统计数据。 The high number of libraries available is one of the many thing that make developing with Python such a fantastic experience. 可用的大量库是使用Python开发这么棒的体验的众多因素之一。

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 : 或者,使用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函数并传递一个而不是sum函数来map

mean = lambda x: sum(x) / float(len(x))
map(mean, zip...)
>>> 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 offer another solution using list comprehension, groupby and reduce . 我只是使用list comprehension, groupbyreduce提供另一种解决方案。 reduce has to be imported from functools in py3.x. reduce了从进口functools在py3.x.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:删除所有值都等于某个值的行 - Pandas: Remove rows where all values equal a certain value 从字典列表返回值列表,其中键等于某个值 - Returning a list of values from a list of dictionaries where keys equal a certain value Python:找到list中元素的所有组合达到某个值 - Python: find all combinations of elements in list to reach a certain value 检查列表中的元素是否等于列表的总和值 - Check if elements in a list equal to the sum value of the list 如何获取字典值中所有元素的数量,其中value是一个列表,给int编号python - How to get number of all elements in values of dictionary, where is value is a list, give the int number python 获取列表中满足一定和值的元素的索引 - Get indices of elements in a list that satisfies certain sum value 访问列表元素不等于特定值 - Access list elements that are not equal to a specific value 如何在列表中找到等于 python 中某个值的所有数字组合? - How can I find all combinations of numbers in a list that's equal to a certain value in python? 我需要一个带有两个键的类似字典的结构,您可以在其中获取所有对象的列表,其中一个具有特定值 - I need a dict-like structure with two keys, where you can get the list of all objects with a certain value of one of them 数一下 Pandas 系列中有多少个初始元素等于某个值? - Count how many initial elements in Pandas Series equal to a certain value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM