PYTHON:查找嵌套列表的平均值
PYTHON: Finding the average of values of a nested list
我有一个列表:
data=[ [["apple",2]], [["cake",5],["cake",8]], [["chocolate",3],["chocolate",9],["chocolate",10]],[["grapes",6]] ]
这个列表(数据)由列表(数据[x])组成,列表(数据[x])由包含相同单词的列表(数据[x][x])组成,每个列表都有一个数字。我想找到每个单词的平均数(任何四舍五入为整数的浮点数)。
例如,列表 (data[1]) 中有 2 个蛋糕,同一个列表中有数字 5 和 8。我想要这两个数字的平均值,即 7,等等data[1] 将更改为 ["cake",7]
如果对列表中的每个元素都这样做,结果应该是:
data=[["apple",2],["cake",7],["chocolate",7],["grapes",6]]
这是怎么做到的?谢谢 :3
您可以在列表理解中使用 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]]
此处的 zip
函数会将您的值与嵌套列表中的名称分开:
>>> [zip(*i) for i in data]
[[('apple',), (2,)], [('cake', 'cake'), (5, 8)], [('chocolate', 'chocolate', 'chocolate'), (3, 9, 10)], [('grapes',), (6,)]]
然后你可以遍历它们并计算 avg 并使用 set
到 select 一组名称!另请注意,你已经使用了 round
函数作为你想要的 avg对于 cake
是 7
因为它的 6.5 .round 函数将为你完成这项工作
你可以试试
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
输出:
[['apple', 2], ['cake', 6], ['chocolate', 7], ['grapes', 6]]
如果使用 python 3:
,则可以使用 2 个列表理解和 numpy.mean
function or 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]]
如果没有均值函数,可以这样计算
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]]
我有一个列表:
data=[ [["apple",2]], [["cake",5],["cake",8]], [["chocolate",3],["chocolate",9],["chocolate",10]],[["grapes",6]] ]
这个列表(数据)由列表(数据[x])组成,列表(数据[x])由包含相同单词的列表(数据[x][x])组成,每个列表都有一个数字。我想找到每个单词的平均数(任何四舍五入为整数的浮点数)。
例如,列表 (data[1]) 中有 2 个蛋糕,同一个列表中有数字 5 和 8。我想要这两个数字的平均值,即 7,等等data[1] 将更改为 ["cake",7]
如果对列表中的每个元素都这样做,结果应该是:
data=[["apple",2],["cake",7],["chocolate",7],["grapes",6]]
这是怎么做到的?谢谢 :3
您可以在列表理解中使用 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]]
此处的 zip
函数会将您的值与嵌套列表中的名称分开:
>>> [zip(*i) for i in data]
[[('apple',), (2,)], [('cake', 'cake'), (5, 8)], [('chocolate', 'chocolate', 'chocolate'), (3, 9, 10)], [('grapes',), (6,)]]
然后你可以遍历它们并计算 avg 并使用 set
到 select 一组名称!另请注意,你已经使用了 round
函数作为你想要的 avg对于 cake
是 7
因为它的 6.5 .round 函数将为你完成这项工作
你可以试试
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
输出:
[['apple', 2], ['cake', 6], ['chocolate', 7], ['grapes', 6]]
如果使用 python 3:
,则可以使用 2 个列表理解和numpy.mean
function or 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]]
如果没有均值函数,可以这样计算
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]]