Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose Variable With Highest Value (Python)

Tags:

python

How do I make Python choose the variable with the hightest integer value?

E.g

a = 1
b = 2
c = 3
d = 0

I want the output to be " c". I know the max() command does something similar, but instead of choosing the variable itself, it will choose the hightest number. If I wasn't clear enough about this please tell me and I'll try and reexplain, but it seems to be a simple enough matter of not knowing the command. Thanks in advance!

like image 454
Nikachuu Avatar asked Dec 01 '25 07:12

Nikachuu


1 Answers

This is a job for a dictionary:

data = {
    'a': 1,
    'b': 2,
    'c': 3,
    'd': 4
}
print max(data, key=data.get)
d

Python does not make a point of tracking variable names.

like image 200
Eric Avatar answered Dec 03 '25 21:12

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!