0

I have an array of values:

increase_pop = [500, -300, 200, 100]

I am trying to find the index of the lowest and highest values. Most of my code works and everything seems to be going well except for one problem. The rest of my code looks like the following:

max_value = increase_pop[0]
min_value = increase_pop[0]

count = 0
while count < len(increase_pop):
    if increase_pop[count] > max_value:
        max_year = count
        max_value = increase_pop[count]
    elif increase_pop[count] < min_value:
        min_value = increase_pop[count]
    count += 1

print(min_value)
print(max_value)

This code gets me the values of the min and max exactly what I want. However, I also want the index values of those positions. So for the max value I have the variable max_year = count assigned to it. Thus, I would think that max_year would be assigned to the count at the position where max_value was found. However, when I do a print(max_year) I get the following error:

UnboundLocalError: local variable 'max_year' referenced before assignment

Does anyone know what my (probably) small/minor issue is? What am I forgetting?

4
  • add max_year= 0 before the loop Commented Oct 4, 2016 at 14:37
  • An alternative way of finding the max/min: max(enumerate(increase_pop), key=lambda x: x[1])[0] Commented Oct 4, 2016 at 14:37
  • Using a while loop like this is good for reasoning about code, but more common Python would be to use for index, pop_count in enumerate(increase_pop): to have both the index and the value. Commented Oct 4, 2016 at 14:37
  • You might want to consider: max_value = max(increase_pop) followed by max_year = increase_pop.index(max_value). You can use the min function to get the minimum. Commented Oct 4, 2016 at 14:38

2 Answers 2

2

You can make your codes more pythonic by using the feature of list:

min_value = min(increase_pop)
max_value = max(increase_pop)
min_value_index = increase_pop.index(min_value)
max_value_index = increase_pop.index(max_value)
Sign up to request clarification or add additional context in comments.

2 Comments

This is also good but one caveat applies if you're dealing with very large sequences: you are scanning the sequence multiple times.
I test the code. seems the function will costs more time than min, max.
2

max_year is assigned when the first if conditional is satisfied. But if that never happens, max_year will never be assigned. That situation will occur when increase_pop[0] (and hence the initial value of max_value) is the largest value in increase_pop: then increase_pop[count] > max_value will never be true.

In your code, you could simply initialize max_year = count = 0

However, IMO the neatest, most Pythonic solution is the one from Patrick Haugh's comment:

max_year, max_value = max( enumerate(increase_pop), key=lambda x: x[1] )

To unpack that: enumerate(increase_pop) generates a sequence of pairs:

(0, increase_pop[0]),   (1, increase_pop[1]), ...

and the max operator takes the "maximum" such pair according to the criterion specified by the key (and that particular key function just says "only consider the second value in each pair").

2 Comments

I was reading about that and I understand that but how do I find the index then of the max_year value then?
@MikeCuddy: this happens when the very first value is the maximum. Just set max_year to 0 before the loop (as you also set the value of max_value there, it makes sense to set the index there as well to keep things consistent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.