Print the highest value variable

Hello,

I know how to find the highest number in a set but i want to know how to get the variable name of that number.
Here is my code:

number1 = 1
number2 = 4
number3 = 3

largest = max(number1,number2,number3)
print largest

It prints “4” but i want it to print “number2” instead. How can I accomplish this?

Thanks,

-Matthew

Wasn’t that asked last week?

anyway, place your data into a list


numbers = [1,4,3]

sort the list


numbers.sort()

#or
sorted(numbers)

the highest number is the last one


highest = numbers[-1]

Or without sorting

just


max(numbers)

I hope it helps
Monster

[edit]

I just see:
You already know about max(). Why would you need to know the name of the variable? It makes not sense. A variable name is no information/data. This means you have some information that you didn’t told us. I guess your are looking for something else (a dict maybe).
I hope it helps

This is not what I wanted, I wanted to print the name of the variable not the number itself. :stuck_out_tongue:

number = [1,4,3]
largest = max(number)

for x, y in enumerate(number):
    if y == largest:
        print("number"+x)

Untested but should probably work and print “number1”

As said, the request itself makes not much sense. Please tell why you think you need this.

this is pretty confusing. if the OP could state the purpose we could possibly recommend a logical way to accomplish what he’s looking for. back in my early days of coding i would have tried something like this thinking that it was the only way to possibly get a variable in to another function. or maybe he’s trying to construct new code at runtime. both of those ALWAYS have alternatives.

as far as this request goes, if he’s looking for just a text representation of his variable’s name, a dictionary, sorted() and the operator module would be one way:


import operator

# Create new dictionary for variable names and values
new_dict = {"number1":1, "number2":4, "number3":3}

# A list of tuples sorted by value
sorted_new_dict = sorted(new_dict.items(), key=operator.itemgetter(1))

# Get largest variable([<i>last tuple</i>][<i>first value</i>])
largest = sorted_new_dict[-1][0]

Output:


&gt;&gt;&gt; largest
'number2'