5

So I would like to get the maximum value from 3 variables, x,y,z.

x = 1
y = 2
z = 3
max(x, y, z)  # returns 3 but I want "z"

However this returns the value of z i.e 3. How do I get the name of the variable e.g. "z" instead?

Thanks

Black Thunder
  • 6,215
  • 5
  • 27
  • 56
oceandye
  • 229
  • 3
  • 6
  • 13
  • the closest you can get is np.argmax; but that only returns the position in an array – Mohammad Athar Sep 06 '18 at 13:18
  • 6
    Variables are name mappings to objects, they don't really exist as strings. What are you attempting to do? – esqew Sep 06 '18 at 13:19
  • Is there a specific reason you need that information? What if multiple variables had the same value? What if one variable with the same value was never given to the max function? – OneCricketeer Sep 06 '18 at 13:19
  • 2
    You should use dictionary if you really want to get the name of maximum variable. `d = {'x': 1, 'y': 2, 'z': 3}` then `max(d, key=d.get)` – Harshita Sep 06 '18 at 13:20
  • This might help https://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string – Space Impact Sep 06 '18 at 13:28
  • I'm trying to find the mode for different categorical responses like yes, maybe, most likely, etc. So I would need the variable name since the actual value would be kinda useless. – oceandye Sep 06 '18 at 13:35

5 Answers5

8

Make a dictionary and then use max. Also works with min. It will return you the variable name in the form of string.

>>> x = 1
>>> y = 2
>>> z = 3
>>> var = {x:"x",y:"y",z:"z"}
>>> max(var)
3
>>> var.get(max(var))
'z'
>>> var.get(min(var))
'x'
>>>
Black Thunder
  • 6,215
  • 5
  • 27
  • 56
  • 1
    Your `max` and `min` calls are comparing the dict keys by string comparison order, not comparing the numerical values. [Even if you set `x = 3`, `y = 2`, and `z = 1`, the `max` call says `z`.](https://ideone.com/5DcnZ0) – user2357112 Sep 07 '18 at 20:42
  • @user2357112 Thanks for my mistake. I have edited my answer. Check now. – Black Thunder Sep 08 '18 at 07:07
5

You could put them into a dictionary and use max with a key:

dict1 = {'x':1, 'y':2, 'z':3}
max(dict1, key=dict1.get)

But think through the ramifications of this. What if you have the same val multiple times, and other edge cases.

dfundako
  • 7,662
  • 3
  • 15
  • 31
2

Use a dictionary and get the maximum of its items, using a key function.

d = {'x':1, 'y':2, 'z':3}
max(d.items(), key=lambda i: i[1])
Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
0

This is not at all recommended but you can use the globals() dictionary which keeps track of all variables that are either built-ins or have been defined by the user:

x = 1
y = 2
z = 3

variables = {k: v for k, v in globals().items() if isinstance(v, int)}
max_value = max(variables.values())
res = next(k for k, v in variables.items() if v == max_value)

which results in:

print(res)  # z
Ma0
  • 14,712
  • 2
  • 33
  • 62
  • What if in the program there are other globals with higher value than the 3 ones that we're comparing? – Natacha Sep 27 '20 at 14:09
-3

Create a List of the numbers then type max() around the list.

ashish trehan
  • 385
  • 4
  • 9