Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何计算列表中单个列表的平均值_Python_Python 3.x - Fatal编程技术网

Python如何计算列表中单个列表的平均值

Python如何计算列表中单个列表的平均值,python,python-3.x,Python,Python 3.x,我有一个包含大量数据的列表: data = [ [4,25,1,6,3,1,6,...],[45,2,1,5,6,20,29],[491,5,195,239,194,09] ..[2,1,5,6,1,95] ] 我一直在想如何计算每个列表的平均值,并将其转换为一列数据,如下所示: List# average 1 (Mean value of the first list) 2 (Mean value of the second list) .... 15

我有一个包含大量数据的列表:

data =  [ [4,25,1,6,3,1,6,...],[45,2,1,5,6,20,29],[491,5,195,239,194,09]
         ..[2,1,5,6,1,95] ]
我一直在想如何计算每个列表的平均值,并将其转换为一列数据,如下所示:

List# average
1     (Mean value of the first list)
2     (Mean value of the second list)
....
15    (mean value of the 15th list)
def average(data)
    for i in range(16):
        total = 0.0
        for x in data[i]:
            total = total + x
    return total / len(data[i])
我试着像这样进入迭代循环:

List# average
1     (Mean value of the first list)
2     (Mean value of the second list)
....
15    (mean value of the 15th list)
def average(data)
    for i in range(16):
        total = 0.0
        for x in data[i]:
            total = total + x
    return total / len(data[i])
但到目前为止,它只计算了第一个列表的平均值。我还想用第一段数据计算每个列表的平均值,但我不知道如何处理列表中的列表


编辑:按建议更改,但我仍然只有一个函数结果,而不是每个子列表的平均值。

x替换numb

你应该一个一个地取消你的返回

不幸的是,根据您的目标,可能会出现更多问题。:)

编辑:我感到很慷慨

def average(listOfList):
    for index, listOfNumbers in listOfList:
        print (  "{}\t{}".format(index, sum(listOfNumbers) / len(listOfNumbers)) ) 

代码的问题是缩进错误。您的
return
语句缩进到
while
循环中。如果你解决了这个问题,它应该会起作用。但是,最好使用一个平均函数来查找每个子列表的平均值,并使用一个列表

def average(l):
    return sum(l)/len(l) #float(len(l)) if python 2.x

new_list = [average(lst[1:]) for lst in list] # since you want to cut off the first value.

此外,您不应将
list
用作变量,因为它会重新分配内置项。

您可以使用内置项
sum
len
函数:

l = [ [4,25,1,6,3,1,6],[45,2,1,5,6,20,29],[491,5,195,239,194,9],[2,1,5,6,1,95]]
new_l = {'List {}'.format(i):sum(a)/float(len(a)) for i, a in enumerate(l, start=1)}
输出:

{'List 4': 18.333333333333332, 'List 2': 15.428571428571429, 'List 3': 188.83333333333334, 'List 1': 6.571428571428571}
[[25, 1, 6, 3, 1, 6], [2, 1, 5, 6, 20, 29], [5, 195, 239, 194, 9], [1, 5, 6, 1, 95]]
{'List 4': 18.33, 'List 2': 15.43, 'List 3': 188.83, 'List 1': 6.57}
编辑:要删除每个列表中的第一个值:

l = [ [4,25,1,6,3,1,6],[45,2,1,5,6,20,29],[491,5,195,239,194,9],[2,1,5,6,1,95]]
new_l = [i[1:] for i in l]
输出:

{'List 4': 18.333333333333332, 'List 2': 15.428571428571429, 'List 3': 188.83333333333334, 'List 1': 6.571428571428571}
[[25, 1, 6, 3, 1, 6], [2, 1, 5, 6, 20, 29], [5, 195, 239, 194, 9], [1, 5, 6, 1, 95]]
{'List 4': 18.33, 'List 2': 15.43, 'List 3': 188.83, 'List 1': 6.57}
编辑:四舍五入字典值:

l = [ [4,25,1,6,3,1,6],[45,2,1,5,6,20,29],[491,5,195,239,194,9],[2,1,5,6,1,95]]
new_l = {'List {}'.format(i):round(sum(a)/float(len(a)), 2) for i, a in enumerate(l, start=1)}
输出:

{'List 4': 18.333333333333332, 'List 2': 15.428571428571429, 'List 3': 188.83333333333334, 'List 1': 6.571428571428571}
[[25, 1, 6, 3, 1, 6], [2, 1, 5, 6, 20, 29], [5, 195, 239, 194, 9], [1, 5, 6, 1, 95]]
{'List 4': 18.33, 'List 2': 15.43, 'List 3': 188.83, 'List 1': 6.57}

其他答案将解决您的问题,只是想让您知道您可以在一行中完成,而无需任何循环:

data=[ [4,25,1,6,3,1,6],[45,2,1,5,6,20,29],[491,5,195,239,194,9],[2,1,5,6,1,95] ]

print(list(map(lambda x:sum(x)/len(x),data)))
#do your stuff with this result
输出:

[6.571428571428571, 15.428571428571429, 188.83333333333334, 18.333333333333332]

return语句缩进到第二个for循环中。它需要在它的外部,在第一个for循环的内部。它是用C写的,所以比你用Python写的要快。是的,我会把它改成data=[[[4,25,1,6,3,1,6,…],[45,2,1,5,6,20,29],[491,5195239194,09]…[2,1,5,6,1,95]。[2,1,5,6,1,95]但是我在你的代码中遇到了错误,不确定出了什么问题。是“lst”=到第1个或第1个?这是从哪里来的?我收到了TypeError:“int”对象是不可编辑的error@Onggia上面发布的代码是有效的,可以运行,但是,您如何在自己的代码中使用它?@哦,别担心,我的列表中有一些不同的名称。您的代码运行良好,谢谢!我正在做的是从我的教科书。但我不清楚你在那里做了什么,你能给我解释一下吗?(我不懂“.format(I)”“a-inenumerate(l,start=1)”)一般来说,我对编码也很陌生,我怎样才能像我的og帖子中所显示的那样在列中显示呢?或者使用不同版本的代码将其输入平均值列表对我来说会更好。@Onggia很乐意帮助!这个问题利用字典理解,允许我们在一行中创建字典。
.format(i)
精确而优雅地将字符插入格式化字符串,其位置由
{}表示
枚举一个iterable上的迭代次数,返回当前索引和对应于该索引的值。如果您有任何问题,请告诉我。我尝试用1代替numb,但仍然没有成功。我的目标是找出每个列表的平均值,并如我所示放入一列中。App感谢您的慷慨,但是我在“{}\t{}”处编译代码时遇到了语法错误。如果您使用的是python 3,请在print参数周围添加括号。我将对其进行修改,以便您理解。