Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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中获得列表中每10个数字的平均值_Python - Fatal编程技术网

如何在python中获得列表中每10个数字的平均值

如何在python中获得列表中每10个数字的平均值,python,Python,如何从列表中获得每10个数字的平均值 下面是示例代码。对每一步重复你的数字列表,例如,在你的案例中为10。计算步骤之间元素的平均值 list [] list contains [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] 无需创建子列表即可计算平均值: >>> my_list = [1, 2, 3,

如何从列表中获得每10个数字的平均值

下面是示例代码。对每一步重复你的数字列表,例如,在你的案例中为10。计算步骤之间元素的平均值

list []
list contains [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

无需创建子列表即可计算平均值:

>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>>> step = 10
>>> for i, _ in enumerate(my_list[::step]):
...     sub_list = my_list[i*10:] if (i+1)*10 > len(my_list) else my_list[i*10:(i+1)*10]  # Condition if the len(my_list) % step != 0
...     print sum(sub_list)/float(len(sub_list))  # Dividing by float' to get decimal value as average (Not needed in Python 3)
... 
5.5
15.5
25.5
testdata = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
        19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] 

LEN = 10

def avg(data):
    datasum = cnt = 0 
    for num in data:
        datasum += num
        cnt += 1
        if cnt == LEN: 
            yield datasum / LEN
            datasum = cnt = 0 
    if cnt: 
        yield datasum / cnt

print(list(avg(testdata)))
# [5.5, 15.5, 25.5]
具有子列表的替代实现:

>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>>> step = 10
>>> for i, _ in enumerate(my_list[::step]):
...     sub_list = my_list[i*10:] if (i+1)*10 > len(my_list) else my_list[i*10:(i+1)*10]  # Condition if the len(my_list) % step != 0
...     print sum(sub_list)/float(len(sub_list))  # Dividing by float' to get decimal value as average (Not needed in Python 3)
... 
5.5
15.5
25.5
testdata = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
        19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] 

LEN = 10

def avg(data):
    datasum = cnt = 0 
    for num in data:
        datasum += num
        cnt += 1
        if cnt == LEN: 
            yield datasum / LEN
            datasum = cnt = 0 
    if cnt: 
        yield datasum / cnt

print(list(avg(testdata)))
# [5.5, 15.5, 25.5]

注意:这是Python3代码,其中int/int是float。在Python2中,int/int是int。

解释代码应该做什么。输入和预期输出是什么。现在代码中发生了什么使您相信它不起作用。最后,请阅读如何组合一个更好的答案,并相应地编辑您的问题。平均每10个数字意味着平均1-10,然后是11-20,依此类推?每10个数字组合产生3000万个平均值!是的,莫努丁。我可以做average=sumlist[0:10]/10,但对于比这个更大的列表,这不是很有效。太棒了!!非常感谢你。