What is the most efficient way to sequentially search for the average and average number of rows in a Python list?
For example, my list:
input_list = [1,2,4,6,7,8]
I want to create a list of results containing:
output_list_mean = [1,1.5,2.3,3.25,4,4.7] output_list_median = [1,1.5,2.0,3.0,4.0,5.0]
If the average is calculated as follows:
- 1 = average (1)
- 1.5 = average (1,2) (i.e. average of the first 2 values โโin input_list)
- 2.3 = average (1,2,4) (i.e. average of the first 3 values โโin input_list)
- 3.25 = average (1,2,4,6) (i.e. the average of the first 4 values โโin input_list), etc.
And the median is calculated as follows:
- 1 = median (1)
- 1.5 = median (1,2) (i.e. median of the first 2 values โโin input_list)
- 2.0 = median (1,2,4) (i.e. median of the first 3 values โโin input_list)
- 3.0 = median (1,2,4,6) (i.e. median of the first 4 values โโin input_list), etc.
I tried to implement it with the next cycle, but it seems very inefficient.
import numpy input_list = [1,2,4,6,7,8] for item in range(1,len(input_list)+1): print(numpy.mean(input_list[:item])) print(numpy.median(input_list[:item]))
source share