Finding min and max values from a dictionary containing tuple values

Viewed 5674

I have a python dictionary named cdc_year_births.

For cdc_year_births, the keys are the unit (in this case the unit is a year), the values are the number of births in that unit:

print(cdc_year_births)
{2000: 4058814, 2001: 4025933, 2002: 4021726, 2003: 4089950, 1994: 3952767, 
1995: 3899589, 1996: 3891494, 1997: 3880894, 1998: 3941553, 1999: 3959417}

I wrote a function that returns the maximum and minimum years and their births. When I started the function, I thought I'd hard code the max and min unit at 0 and 1000000000, respectively, and then iterate through the dictionary and compare each key's value to those hard coded values; if the conditions were met, I'd replace the max/min unit and the max/min birth.

But if the dictionary I used had negative values or values greater than 1000000000, this function wouldn't work, which is why I had to "load in" some actual values from the dictionary with the first loop, then loop over them again.

I built this function but could not get it to work properly:

def max_min_counts(data):
    max_min = {}
    for key,value in data.items():
        max_min["max"] = key,value
        max_min["min"] = key,value
    for key,value in data.items():
        if value >= max_min["max"]:
            max_min["max"]=key,value
        if value <= max_min["min"]:
            max_min["min"]=key,value
    return max_min

t=max_min_counts(cdc_year_births)
print(t)

It results in TypeError: unorderable types: int() >= tuple() for

if value >= max_min["max"]:

and

if value <= max_min["min"]:

I tried extracting the value from the tuple as described in Finding the max and min in dictionary as tuples python, but could not get this to work.

Can anyone help me make the second, shorter function work or show me how to write a better one?

Thank you very much in advance.

4 Answers

Yeah, I'm up to this exercise too.

Without using max and min functions (we haven't covered them yet in the course material) here's the hard way...

def minimax(dict):
    minimax_dict = {}
    if(len(dict) == 31):
        time = "day_of_month"
    elif(len(dict) == 12):
        time = "month"
    elif(len(dict) == 7):
        time = "day_of_week"
    else:
        time = 'year'
    min_time = "min_" + time
    max_time = "max_" + time
    for item in dict:
        if 'min_count' in minimax_dict:
            if dict[item] < minimax_dict['min_count']:
                minimax_dict['min_count'] = dict[item]
                minimax_dict[min_time] = item
        else:
            minimax_dict['min_count'] = dict[item]
            minimax_dict[min_time] = item
        if 'max_count' in minimax_dict:
            if dict[item] > minimax_dict['max_count']:
                minimax_dict['max_count'] = dict[item]
                minimax_dict[max_time] = item
        else:
            minimax_dict['max_count'] = dict[item]
            minimax_dict[max_time] = item
    return minimax_dict
#here's the test stuff...
min_max_dow_births = minimax(cdc_dow_births)
#min_max_dow_births
min_max_year_births = minimax(cdc_year_births)
#min_max_year_births
min_max_dom_births = minimax(cdc_dom_births)
#min_max_dom_births
min_max_month_births = minimax(cdc_month_births)
#min_max_month_births
Related