![](/img/trans.png)
[英]error list indices must be integers, not list. Take values of one array use them as indexes to delete values from another array
[英]Take the mean values of a number of integers in one list, based on the integers in another list
我有一个包含大量整数的列表,然后另一个列表也包含整数(如果加在一起)总和为第一个列表中的整数总数。 我希望创建一个 function 迭代第二个列表,并且对于第二个列表中的每个数字,取第一个列表中的值的平均值并重复第二个列表中的所有整数......制作一个最后的第三个列表包含所需的平均值。
例如:我的两个列表中的一小部分如下所示: [20, 15, 20, 30, 40, 20, 10, 8]
, [2, 3, 1, 2]
因此,由于 2 是我第二个列表中的第一个数字,我想取第一个列表中前两个整数的平均值,然后是下一个 3,依此类推,并将它们添加到第三个列表中。
这是我的想法的简要说明,但显然不完整。
def mean_values(list_of_numbers, numbers_for_meanvalue):
list_of_means = []
for i in numbers_for_meanvalue:
mean = sum(list_of_numbers[0]+...+list_of_numbers[i])/numbers_for_meanvalue[i]
list_of_means.append[mean]
return list_of_means
您可以获取列表的一部分: list[start: end]
。
def mean_values(list_of_numbers, numbers_for_meanvalue):
list_of_means = []
last_start = 0
for num in numbers_for_meanvalue:
mean = sum(list_of_numbers[last_start:last_start + num]) / len(list_of_numbers[last_start:last_start + num])
last_start += num
list_of_means.append[mean]
return list_of_means
如果我对你的理解正确,我不会,但是:
>>> a = [20, 15, 20, 30, 40, 20, 10, 8]
>>> b = [2, 3, 1, 2]
>>> n = 0
>>> for e in b:
... sl = a[n:n+e]
... print(sl,(sum(sl) / e))
... n += e
...
[20, 15] 17.5
[20, 30, 40] 30.0
[20] 20.0
[10, 8] 9.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.