lessthanl0l
lessthanl0l

Reputation: 1095

Finding MIN & MAX values across 3 different dictionaries

I have 3 years of statistic data stored in 3 dictionaries. I want to find the min value in each month across 3 years. For example, for January it would be 10. And for February it would be 15.

I would need to store the results in a new dictionary. For example, {"January": 12, "February": 15, ...}

How should I proceed?

stat2011 = {"January": 12, "February": 20, "March": 50, "April": 70, "May": 15,
           "June": 35, "July": 30, "August": 15, "September": 20, "October": 60,
           "November": 13, "December": 50}

stat2012 = {"January": 36, "February": 15, "March": 50, "April": 10, "May": 90,
           "June": 25, "July": 35, "August": 15, "September": 20, "October": 30,
           "November": 10, "December": 25}

stat2013 = {"January": 10, "February": 60, "March": 90, "April": 10, "May": 80,
           "June": 50, "July": 30, "August": 15, "September": 20, "October": 75,
           "November": 60, "December": 15}

Upvotes: 1

Views: 155

Answers (4)

lessthanl0l
lessthanl0l

Reputation: 1095

There's a neat way to do this using collections.

from collections import Counter

stat2011 = {"January": 12, "February": 20, "March": 50, "April": 70, "May": 15,
           "June": 35, "July": 30, "August": 15, "September": 20, "October": 60,
           "November": 13, "December": 50}

stat2012 = {"January": 36, "February": 20, "March": 50, "April": 10, "May": 90,
           "June": 25, "July": 35, "August": 15, "September": 20, "October": 30,
           "November": 10, "December": 25}

stat2013 = {"January": 10, "February": 60, "March": 90, "April": 10, "May": 80,
           "June": 50, "July": 30, "August": 15, "September": 20, "October": 75,
           "November": 60, "December": 15}

print dict(Counter(stat2011) & Counter(stat2012) & Counter(stat2013))    # MIN
print dict(Counter(stat2011) | Counter(stat2012) | Counter(stat2013))    # MAX

Upvotes: 4

tinybike
tinybike

Reputation: 562

Here's one way to do it:

minstat = {}
for month in stat2011:
    minstat[month] = min(stat2011[month], stat2012[month], stat2013[month])

The other answers are cleverer than mine, but sometimes the least clever solution is the easiest to read :)

Upvotes: 2

thefourtheye
thefourtheye

Reputation: 239573

months = ['February', 'October', 'January', 'April', 'November',
'March', 'August', 'May', 'December', 'June', 'September', 'July']

result, data = {}, [stat2011, stat2012, stat2013]
for month in months:
    result.setdefault(month, {})
    result[month]["max"] = max(year_data[month] for year_data in data)
    result[month]["min"] = min(year_data[month] for year_data in data)

print result

Output

{'April': {'max': 70, 'min': 10},
 'August': {'max': 15, 'min': 15},
 'December': {'max': 50, 'min': 15},
 'February': {'max': 60, 'min': 15},
 'January': {'max': 36, 'min': 10},
 'July': {'max': 35, 'min': 30},
 'June': {'max': 50, 'min': 25},
 'March': {'max': 90, 'min': 50},
 'May': {'max': 90, 'min': 15},
 'November': {'max': 60, 'min': 10},
 'October': {'max': 75, 'min': 30},
 'September': {'max': 20, 'min': 20}}

You can use collections.Counter like this to get the maximum and minimum

To get the maximum,

print reduce(lambda x, y: x | Counter(y), data, Counter())

# Counter({'March': 90, 'May': 90, 'October': 75, 'April': 70, 'February': 60, 'November': 60, 'June': 50, 'December': 50, 'January': 36, 'July': 35, 'September': 20, 'August': 15})

To get the minimum

print reduce(lambda x, y: x & Counter(y), data[1:], Counter(data[0]))

# Counter({'March': 50, 'October': 30, 'July': 30, 'June': 25, 'February': 20, 'September': 20, 'August': 15, 'May': 15, 'December': 15, 'January': 10, 'April': 10, 'November': 10})

Upvotes: 2

zhangxaochen
zhangxaochen

Reputation: 34027

In [2]: stats=[stat2011, stat2012, stat2013]

In [3]: min(i['January'] for i in stats)
Out[3]: 10

In [4]: min(i['February'] for i in stats)
Out[4]: 15

If you wanna store the min values of each month in a dict:

In [8]: {k:min(i[k] for i in stats) for k in stat2011}
Out[8]: 
{'April': 10,
 'August': 15,
 'December': 15,
 'February': 15,
 'January': 10,
 'July': 30,
 'June': 25,
 'March': 50,
 'May': 15,
 'November': 10,
 'October': 30,
 'September': 20}

Upvotes: 5

Related Questions