简体   繁体   English

PYTHON:查找嵌套列表的平均值

[英]PYTHON: Finding the average of values of a nested list

I have a list: 我有一个清单:

    data=[ [["apple",2]], [["cake",5],["cake",8]], [["chocolate",3],["chocolate",9],["chocolate",10]],[["grapes",6]] ]

This list (data) consists of lists (data[x]), which consists of lists (data[x][x]) containing the same word together, each with a number with it. 该列表(数据)由列表(data [x])组成,列表由包含相同单词的列表(data [x] [x])组成,每个列表都带有一个数字。 I want to find the average of the number for each word (any floats rounded to integer). 我想找到每个单词的平均数(任何浮点数舍入为整数)。

For example, there are 2 cakes in the list (data[1]) and in the same list are the numbers 5 and 8. I want the average out of those two numbers, which would be 7, and so data[1] would be changed to ["cake",7] 例如,列表中有2个蛋糕(数据[1]),同一个列表中有数字5和8.我想要这两个数字中的平均值,即7,因此数据[1]会改为[“蛋糕”,7]

If this is done for each element in the list, the result should be: 如果对列表中的每个元素执行此操作,则结果应为:

    data=[["apple",2],["cake",7],["chocolate",7],["grapes",6]]

How could this be done? 怎么可以这样做? Thanks :3 谢谢:3

You can use zip within a list comprehension: 您可以在列表理解中使用zip

>>> from __future__ import division
>>> [[set(i).pop(),round(sum(j)/len(j),0)] for i,j in [zip(*i) for i in data]]
[['apple', 2.0], ['cake', 7.0], ['chocolate', 7.0], ['grapes', 6.0]]

The zip function here would separate your values from names in nested lists: 这里的zip函数会将您的值与嵌套列表中的名称分开:

>>> [zip(*i) for i in data]
[[('apple',), (2,)], [('cake', 'cake'), (5, 8)], [('chocolate', 'chocolate', 'chocolate'), (3, 9, 10)], [('grapes',), (6,)]]

Then you can loop over them and calculate the avg and use set to select the set of names!also note that is have used round function as you wanted the avg for the cake to be 7 as its 6.5 .the round function will do the job for you 然后你可以循环它们并计算平均值和使用set来选择名称集!还要注意它已经使用了round函数,因为你想要cake的平均值为7作为其6.5。圆函数将完成工作为了你

You can try 你可以试试

x=[ [["apple",2]], [["cake",5],["cake",8]], [["chocolate",3],["chocolate",9],["chocolate",10]],[["grapes",6]] ]
y=[]
for i in x:
    avg=0
    c=0
    for k in i:
        avg=k[1]+avg
        c=c+1
    avg=avg/c
    y.append([k[0],avg])
    avg=0
    c=0

print y

Output: 输出:

[['apple', 2], ['cake', 6], ['chocolate', 7], ['grapes', 6]]

You can do it with 2 list comprehensions and numpy.mean function or statistics.mean function if you use python 3: 如果使用python 3,可以使用2个列表numpy.meannumpy.mean函数或statistics.mean函数来完成:

In [1]: [[x[0][0], round(np.mean([y[1] for y in x]))] for x in data]
Out[1]: [['apple', 2.0], ['cake', 7.0], ['chocolate', 7.0], ['grapes', 6.0]]

If you don't have mean function, you can calculate it like this 如果你没有平均功能,你可以像这样计算它

In [2]: tmp = ([x[0][0], [y[1] for y in x]] for x in data)
In [3]: [[x[0], round(sum(x[1])*1.0/len(x[1]))] for x in tmp]
Out[3]: [['apple', 2.0], ['cake', 7.0], ['chocolate', 7.0], ['grapes', 6.0]]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM