0

I am trying to find the average values for each month by using a function that references a sorted list of tuples containing (date,value) where date is a string and in the format yyyy-mm-dd and value is a float. The first entry in the data set is not showing up and I believe there may be more errors that I am not aware of. This is what I have so far in Python (note, previous functions not included):

def value_avg(previous_tuple_list):
    start_month=tup_list[0][0]
    start_month1=start_month.split("/")
    start_month2=start_month1[1]
    value_total=0
    value_count=0
    value_avglist=[]
for item in tuple_list:
    value_month=tup[0]
    value_month1=value_month.split("/")
    value_month2=value_month1[1]
    value=item[1]
    if value_month2==start_month2:
        value_sum += value
        value_count += 1
    else:
        value_avg=(value_sum / value_count)
        date_no_day= item[0][:7]
        value_avg_tup=(value_avg, date_no_day)
        value_avglist.append(value_avg_tup)
        value_sum -= value_sum
        value_count -= value_count
        start_month2 = value_month2
user3699546
  • 133
  • 1
  • 7

2 Answers2

0

If I understand your problem correctly, why don't you create a dictionary with the keys being the months and the values being a list of the values in that month. Then find the average for each month. So, something like:

from collections import defaultdict

def value_avg(previous_tuple_list):
    months = defaultdict(list)
    for entry in previous_tuple_list:
        month = entry[0].split('-')[1]
        months[month].append(entry[1])

    averages = {month : sum(values) / len(values) for month, values in months}

I haven't tested the code above but it should give you an idea. The months dictionary then holds (after the for statement is complete) the months as keys with all the values for each month as values in a list. Finally, the averages dictionary has a key for each month and the average as the value of each key.

To do this without the use of dictionaries:

You can use 2 lists, each having 12 elements (because 12 months) and update their values as you go through the tuples. One list will hold the sum of values for each month (i.e. January will be at index 0) and the other will hold the number of values for each month. Then you can have a third list (averages) that will have the average of each month at the appropriate index:

def value_avg(previous_tuple_list):
    sums = [0] * 12
    counts = [0] * 12

    for entry in previous_tuple_list:
        month = int(entry[0].split('-')[1]) - 1 # because list indices are 0-based
        sums[months] += entry[1]
        counts[months] += 1

    averages = [sum / count for sum, count in zip(sums, counts)]

P.S. I haven't tested either of the codes above; I'm hoping they would point you into the right direction.

s16h
  • 4,647
  • 1
  • 21
  • 33
-1

A little bit hard to understand what you want but....

if the tuples are sorted by month you could simply do:

avg = []
month = 1
for x in tuples:
   if x[0].split('/')==month: 
       avg[month-1] += x[1]
 else: 
       avg[month-1] = count[month-1]/count

You should also work on how you label variables and such. For example: "Tuple_List"??? There are both tuple and object types in python so this becomes very confusing.

value_month=tup[0]
value_month1=value_month.split('/')
etc.

should just be

month1 = tup[0].split('/')[0]
month2 = tup[0].split('/')[1]
user3727843
  • 574
  • 1
  • 4
  • 13
  • What is `if x[0].split('/')==month:` supposed to achieve? Also, format your code properly, the `else` statement can belong to the `if` or to the `for`; at the moment, it's in between. – s16h Jun 10 '14 at 22:38