4

Recently tried something similar to PHP get largest number from 3 variables

(In Python's form of course) However when returning the value I get the value of the variable, not the name of the variable.

My array looks like this:

x = 1

y = 2

z = 3

alloutputs = [x, y, z]

The furthest and most ugliest before needing help is this:

alloutputs[alloutputs.index(max(alloutputs))]

However it's still giving me an integer of the highest value! How would I get back the name of x, y, or z depending on which is largest?

Community
  • 1
  • 1
sunshinekitty
  • 2,307
  • 6
  • 19
  • 31
  • 3
    Why would you need the name of the variable, as opposed to the value? There's probably a better option, e.g. turning those variables into dictionary entries. –  Feb 18 '14 at 14:04
  • 2
    Variables are just temporary symbols; they have no meaning outside of the algorithm, and the symbol used for them is arbitrary. – 2rs2ts Feb 18 '14 at 14:12

5 Answers5

10

As @zhangxaochen says, you need to use a dictionary or named tuple. You could use a dictionary like this:

>>> d = {'x': 1, 'y': 2, 'z': 3}
>>> max(d, key=d.get)
'z'
Noel Evans
  • 8,113
  • 8
  • 48
  • 58
2

A list does not save the names of variables. Use a dictionary or namedtuple:

In [296]: d=dict(x=1, y=2, z=3)

In [297]: from operator import itemgetter
     ...: max(d.items(), key=itemgetter(1))[0]
     ...: 
Out[297]: 'z'
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
2
x, y, z = 1, 2, 3
alloutputs = ["x", "y", "z"]
print max(alloutputs, key = locals().get)

However, if you ever really have to do this, you might want to reconsider your design, IMHO.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 2
    It turns into perfectly good design when you use a proper, separate dictionary in place of `locals()`. –  Feb 18 '14 at 14:13
  • @delnan You mean, depending on the variable's name is considered a good design? – thefourtheye Feb 18 '14 at 14:15
  • Not at all. I'm talking about when it *isn't* a variable name, simply a dictionary key (and most likely not hard-coded but coming from a dynamic data source). –  Feb 18 '14 at 14:16
  • @delnan But OP is trying to get the variable's name which has got the maximum value. That is why I want him to reconsider the design. – thefourtheye Feb 18 '14 at 14:17
  • I concur, I already told OP the same thing in a comment on the question. –  Feb 18 '14 at 14:18
1

Disclaimer: this is terrible and you should not do this.

[var for var in dir() if eval(var) == max(all)][0]

It does what you want but - as others have said - you would be much better off using a dictionary.

Max
  • 21,123
  • 5
  • 49
  • 71
0

You can use dictionary:

alloutputs = {'x':1,'y':2,'z':3}
maxvalue = max(alloutputs.values())
print [k for k, v in alloutputs.iteritems() if v == maxvalue]
Bardia Heydari
  • 777
  • 9
  • 24
  • 3
    Looping over a dictionary is like clubbing someone to death with a loaded Uzi. Use the `key` parameter of `max`, e.g. `max(alloutputs, key=alloutputs.get)` or something to that effect. –  Feb 18 '14 at 14:10