1
modifiedL = [('10-1984', 2.8), ('10-1984', 2.8), ('10-1984', 2.85), ('10-1984', 2.82), ('10-1984', 2.78), ('10-1984', 2.75), ('09-1984', 2.82), ('09-1984', 2.9), ('09-1984', 2.9), ('09-1984', 2.94), ('09-1984', 2.99), ('09-1984', 3.02), ('09-1984', 3.05), ('09-1984', 3.04), ('09-1984', 3.11), ('09-1984', 3.22), ('09-1984', 3.13), ('09-1984', 3.09), ('09-1984', 2.94), ('09-1984', 3.02), ('09-1984', 2.97), ('09-1984', 2.98)]

avg={}

for date, value in modifiedL:
    avg.setdefault(date, []).append(value)

for key, value in avg.items():
    avg[key] = sum(value)/float(len(value))

After first loop:

avg = {'10-1984': [2.8, 2.81, 2.78, 2.77, 2.84, 2.95, 2.92, 2.85, 2.88, 2.88, 2.8, 2.68, 2.7, 2.56, 2.67, 2.68, 2.77, 2.8, 2.8, 2.85, 2.82, 2.78, 2.75], '09-1984': [2.82, 2.9, 2.9, 2.94, 2.99, 3.02, 3.05, 3.04, 3.11, 3.22, 3.13, 3.09, 2.94, 3.02, 2.97, 2.98]}

After second loop:

avg= {'10-1984': average, '09-1984': average}

Is there a way to simplify the code or perform this in one line?

ehls39119
  • 21
  • 3

1 Answers1

0

Your code is fine. You can replace some parts of it (like using defaultdict or statistics.mean), but it will still take 2 loops. There is no practical reason to make every code a one-liner, it is always better to just make a function with a good name.

However, there is some options to make it one-liner. You can use itertools.groupby (note that this increases algorithm complexity to O(N log N) :

In [11]: from itertools import groupby

In [12]: from operator import itemgetter

In [13]: from statistics import mean

In [14]: {k: mean(map(itemgetter(1), v)) for k, v in groupby(sorted(modifiedL, key=itemgetter(0)), key=itemgetter(0))}
Out[14]: {'09-1984': 3.0075, '10-1984': 2.8}

You can write your version of groupby which does not require sequence to be sorted to reduce complexity back to O(N) (see this answer for examples)

Or if you're using pandas in your project you can just convert the list to a DataFrame:

In [29]: import pandas as pd

In [30]: df = pd.DataFrame(modifiedL)

In [31]: df.groupby(0).mean()
Out[31]:
              1
0
09-1984  3.0075
10-1984  2.8000
awesoon
  • 32,469
  • 11
  • 74
  • 99