[英]finding total average list of lists python
I am new to python, and I need help with finding averages, variances, and grand average in a list of lists.我是 Python 新手,我需要帮助来在列表列表中查找平均值、方差和总平均值。 I have a list of lists like this:我有一个像这样的列表:
allgroups = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
and I am able to find the averages and variances, but for the grand average, I have a problem to find the solution: here is my code:我能够找到平均值和方差,但是对于大平均值,我有一个问题来找到解决方案:这是我的代码:
def avg(allgroups):
return [float(sum(i)) / len(i) for i in allgroups]
def variance(allgroups):
return [sum((x - sum(group) / len(group)) ** 2 for x in group) / (
len(group) - 1) for group in allgroups]
def calcavg(allgroups):
return float(sum(avg(allgroups) / len(avg(allgroups))))
TheAvg = avg(allgroups)
print(TheAvg)
Variance = variance(allgroups)
print(Variance)
calcAvg = calcavg(allgroups)
print(calcAvg)
I keep getting the screen error:我不断收到屏幕错误:
return float(sum(avg(allgroups) / len(avg(allgroups))))
TypeError: unsupported operand type(s) for /: 'list' and 'int'
When I do the code:当我执行代码时:
average= sum(TheAvg)/len(TheAvg)
I could find the grand average, but when I do我可以找到总平均值,但是当我这样做时
def calcavg(allgroups):
return float(sum(avg(allgroups) / len(avg(allgroups))))
I keep getting an error.我不断收到错误消息。 I will be grateful for any help.我将不胜感激任何帮助。 P/S: I can't use any library for this problem such as numpy or statistic. P/S:我不能使用任何库来解决这个问题,例如 numpy 或 statistic。
You have a typo in your calcavg
function.您的calcavg
函数中有一个错字。 You did not properly inclose the sum
operator on the left-hand side.您没有正确包含左侧的sum
运算符。 The function should be as shown below:功能应该如下图所示:
def calcavg(allgroups):
return float(sum(avg(allgroups)) / len(avg(allgroups)))
calcavg(allgroups)
>> 6.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.