Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have a long list of tuples that I need to sort eg. [('12/2010', 196.9876),('12/2010', 654.9876), ('11/2010', 234.9876).........]

I have a long list of tuples that I need to sort eg. [('11/2010', 196.9876),('11/2010', 654.9876), ('12/2010', 234.9876).........]

I want to use the date(1st element) to group the 2nd element in the tuples into separate lists. So far I have gotten a list of uniquedates eg [11/2010,12/2010....] and am trying to use these to reference the larger list and do the math, ive come up with:

vol_new = []
for monthrow in unique_date_list: 
    for row in date_and_av_tup: 
        if monthrow == row[0]:
            vol_new.append(row[1])
            len_vol_new = len(vol_new) # finds the n of items in volume list
            my_vol_total_new = reduce(lambda y,x: x+y, vol_new) # adds everything in the volume list
            average_vol_new = float(my_vol_total_new) / float(len_vol_new) #stores the average volume in a list

print average_vol_new

This might be really rubbish code but I'm new to coding and am getting very frustrated trying to do this, thanks for any help offered.

PS I'm using Python

like image 780
user1897635 Avatar asked Dec 03 '25 11:12

user1897635


2 Answers

You might find the pandas data analysis library useful for this to create a table on which you can easily do these functions. For example:

import pandas as pd

months = [('11/2010', 196.9876),('11/2010', 654.9876), ('12/2010', 234.9876)]
df = pd.DataFrame(months, columns=['Month', 'Value'])

df is a DataFrame (i.e. a table) that looks like:

    Month   Value
0    11/2010     196.9876
1    11/2010     654.9876
2    12/2010     234.9876

You can get the averages and totals by using groupby:

[7]:  df.groupby('Month').mean()
Out[7]:         Value    
        Month   
        11/2010  425.9876
        12/2010  234.9876

In [8]: df.groupby('Month').sum()
Out[8]:          Value
        Month   
        11/2010  851.9752
        12/2010  234.9876
like image 97
Matti John Avatar answered Dec 06 '25 03:12

Matti John


Use a defaultdict for grouping.

from collections import defaultdict
d = defaultdict(list)
for date, val in unique_date_list:
    d[date].append(val)
result = d.items()

Edit

Upon rereading your question, I saw you want only the values grouped according to date, without the date. In that case, the last line in my example will be

result = d.values()

Edit 2

As Geoff pointed out, the OP wanted the lists in sorted order. Than the last line will be:

result = [sorted(vals) for _, vals in sorted(d.iteritems(), key=lambda x : x[0])]
like image 24
StoryTeller - Unslander Monica Avatar answered Dec 06 '25 03:12

StoryTeller - Unslander Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!