列表中元素的平均值,按列表中的第一项分组 [英] Average of elements in a list of list grouped by first item in the list

查看:30
本文介绍了列表中元素的平均值,按列表中的第一项分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表看起来像 my_list = [['A',6,7],['A',4,8],['B',9,3],['C',1,1]],['B',10、7]]

我想找到每个内部列表中其他两列的平均值,该平均值由每个内部列表中的第一列分组.

I want to find the averages of the other two columns in each of the inner lists grouped by the first column in each of the inner list.

[['A',5,7.5],['B',9.5,5],['C',1,1]]

['A',5,7.5] 来自 ['A',(6 + 4)/2,(7 + 8)/2]

我不介意最终得到一本字典之类的东西,但我希望它保留在清单中.

I don't mind if I end up getting a dictionary or something, but I would prefer it remain a list.

我尝试了以下操作:

  1. my_list1 = [i [0] for my_list中的我]my_list2 = [对于我在my_list中的i [1:]new_dict = {k:k中的v,k中的v(my_list1,my_list2)}

拆分原始列表,因此第一列变为KEY,第二列和第三列变为VALUE,并将其转换为字典将为您提供汇总,但问题是

SPLITTING THE ORIGINAL LIST SO the first column becomes KEY, and the second and third columns becomes VALUE, and converting it to a dictionary will give you the aggregate but the problem is

我想保留十足的位置,它会堆积起来并为我提供代替浮点值的全部数字

my_list1 = ['A','A','B','C','B']

my_list2 = [[6,7],[4,8],[9,3],[1,1],[10,7]]

new_dict = {'A':[5,8],'B':[10,5],'C':[1,1]}

当我理想中想要的是, [['A',5,7.5],['B',9.5,5],['C',1,1]] (不要介意它是否是字典)

when what I would ideally want is, [['A', 5, 7.5], ['B', 9.5, 5], ['C', 1, 1]] (Don't mind if its a dictionary)

  1. 也许使用for循环思维将第二和第三列转换为浮点数,然后当我将其转换为字典时,它将给我浮点数.但是没有区别,它汇总并给出了一个整数

  1. Converted the second and third columns to float maybe using a for loop thinking, then it will give me a float when I convert it to a dictionary.. But no difference, IT ROUNDS UP and gives a A WHOLE NUMBER.

for i in range(0, len(my_list)):
  for j in range(1, len(my_list[i])):
    my_list[i][j].astype(float)

dict = {}

for l2 in my_list:
  dict[l2[0]] = l2[1:]


我需要保留小数位的原因是因为第二和第三列引用了x和y坐标.


The reason I need to preserve the decimal places is because the second and third columns refer to x and y coordinates..

因此,总的来说,目的是在每个内部列表中找到每个内部列表中其他两列的平均值,并在每个内部列表中以第一列的形式尽可能多地保留小数位

So all in all the objective is to find the averages of the other two columns in each of the inner lists grouped by the first column in each of the inner list with as many decimal places as possible

推荐答案

假设您打算使用以下列表:

Assuming you meant to use the following list:

In [4]: my_list = [['A', 6, 7], ['A', 4, 8], ['B', 9, 3], ['C', 1, 1], ['B', 10, 7]]

只需使用 defaultdict 将第一个元素分组,然后找到平均值:

The simply use a defaultdict to group by the first element, then find the mean:

In [6]: from collections import defaultdict

In [7]: grouper = defaultdict(list)

In [8]: for k, *tail in my_list:
    ...:     grouper[k].append(tail)
    ...:

In [9]: grouper
Out[9]:
defaultdict(list,
            {'A': [[6, 7], [4, 8]], 'B': [[9, 3], [10, 7]], 'C': [[1, 1]]})

In [10]: import statistics

In [11]: {k: list(map(statistics.mean, zip(*v))) for k,v in grouper.items()}
Out[11]: {'A': [5, 7.5], 'B': [9.5, 5], 'C': [1, 1]}

请注意,如果您使用的是Python 2,则无需在 map 之后调用 list .另外,您应该使用 iteritems 而不是 items .

Note, if you are on Python 2, no need to call list after map. Also, you should use iteritems instead of items.

此外,您将必须执行以下操作:

Also, you will have to do something like:

for sub in my_list:
    grouper[sub[0]].append(sub[1:])

代替Python 3上的更清洁版本.

Instead of the cleaner version on Python 3.

最后,Python 2中没有 statistics 模块.因此,只需执行以下操作:

Finally, there is no statistics module in Python 2. So just do:

def mean(seq):
    return float(sum(seq))/len(seq)

,并使用该 mean 代替 statistics.mean

这篇关于列表中元素的平均值,按列表中的第一项分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆