Nick Nick - 9 months ago 10
Python Question

Using a for loop to compare if current value is less than previous


I'm utilizing 2 dictionaries. For my purposes, the first governor_db is a dictionary contains a governor's name as the key and the values are just sets of information that go with it like the year they served etc. The second is a dictionary that contains the year as the key and the values are the unemployment for each month of that year (the key).

What I'm currently working on:

The function I'm working on calls a previous function I used to calculate the average unemployment per governor during the years they served. What I have (below) is iterating through the name of the governor and pulling the average unemployment into this new function to be able to compare what the lowest average unemployment is and who the governor was with that piece of information.

So far what I have will either iterate through it and give me the lowest unemployment accurately , but won't give the right governor that goes with that lowest value.

My current code:

def governor_lowest_avg_unemployment(g_db,unemployment_db):
for governor in g_db.keys():
avg_unemploy= avg_unemployment_for_governor(g_db,unemployment_db, governor)
if avg_unemploy==None:
all_unemploy[state]= avg_unemploy
lowest_avg= min(all_unemploy.values())
for key, value in all_unemploy.items():
if value == lowest_avg:
lowest_gov= key,value

What I want to try to do, is as I'm iterating through that dictionary and it's corresponding unemployment value (stored in dictionary all_unemploy above), I want to be able to store the previous value and compare if the new value I am iterating through is smaller than the previous one. If the new value is smaller than the previous, I want to replace the previous one with the new one and go one until it stops iterating through the dictionary and give me the right tuple combination of lowest avg of : (Governor, average unemployment)

Idea I started was to do something like the following. I'm not sure how to implement it though:

for key, value in all_unemploy:
if value> previous
elif value == previous
elif value = None
elif value < previous
lowest = key, value
return lowest

None in this case just means that for the previous function, there wasn't enough information in the unemployment database to calculate that governor's average unemployment rate.

How would I be able to compare the values like so, and end up getting an accurate lowest value ? Am I on the right track with my idea of implementing such a thing?


If I understood correctly what you are trying to do, the following code should work for you:

def governor_lowest_avg_unemployment(g_db,unemployment_db):
    all_unemploy = {}
    lowest_avg = 1e99    # some huge number, obviously above max possible
    lowest_gov = None
    for  governor in g_db.keys():
        avg_unemploy = avg_unemployment_for_governor(g_db,unemployment_db, governor)
        if avg_unemploy==None:
            all_unemploy[governor] = avg_unemploy
            if avg_unemploy < lowest_avg:
                lowest_avg = avg_unemploy
                lowest_gov = governor
    return lowest_gov