this is a question about python programming I have a list li
this is a question about python programming
I have a list like this
how to make a list like [the average value of 0-11, the average value of 12-23, ......] using a function : annual_average(L):
#the list have 1008 values
Solution
Below is sample code how you can do that. I am just storing some values in my \'mainlist\' variable. You can load it with your data and use this code . Please do rate the answer if it helped. Thank you ver much.
#function to return the average for a list of elements, pass 12 elements to get annual average
 def annual_average(list1):
 print list1
 avg=sum(list1)/float(len(list1)) #convert to float so that we dont loose the fraction part
 return avg
#this is the main list containing data from file , load it from file
 mainlist=[2,3,5,4,6,7,1,8,9,5,6,3,242,22,44,11,4,56,3,1,6,3,1,6,2,1,6,4];
  output=[] #this is the output list containg averages for 0-11,12-23,24-35 etc, initially empty
i=0
while i< len(mainlist):
 sublist=mainlist[i:i+12] #make a sublist from the mainlist
 avg=annual_average(sublist) #calculate average using the function
 output.append(avg) #append this new average into output list
 i=i+12 #increment i by 12 so that we can next set of values in next iteration
 print \"The list of annual averages is \",output
output
[2, 3, 5, 4, 6, 7, 1, 8, 9, 5, 6, 3]
[242, 22, 44, 11, 4, 56, 3, 1, 6, 3, 1, 6]
[2, 1, 6, 4]
The list of annual averages is [4.916666666666667, 33.25, 3.25]
![this is a question about python programming I have a list like this how to make a list like [the average value of 0-11, the average value of 12-23, ......] usin this is a question about python programming I have a list like this how to make a list like [the average value of 0-11, the average value of 12-23, ......] usin](/WebImages/2/this-is-a-question-about-python-programming-i-have-a-list-li-970839-1761495989-0.webp)
