0

You have got three variables like

   a = 1
   b = 2
   c = 3

and you find the minimum can you somehow display the variable name instead of the value it has

Example

   d = min[a,b,c] 

after this operation d should become c since c is the greatest. Or maybe there's some alternative way because i want to assign the variable name in another operation afterwards.

1
  • 1
    "d should become c" -- ignoring the fact that it should become a, since a is the smallest... what do you mean by that? do you want it to be the string 'c'? Commented Apr 8, 2014 at 15:43

2 Answers 2

5

Put variables into a dictionary and call min() with specifying dictionary get as a key:

>>> a = 1
>>> b = 2
>>> c = 3
>>> data = {'a': a, 'b': b, 'c': c}
>>> min(data, key=data.get)
'a'
Sign up to request clarification or add additional context in comments.

Comments

3

You shouldn't do that. Instead, you should do something like:

data = {
  'a' : 1
  'b' : 2
  'c' : 3
}

# get the minimum item, by the value.
smallest = min(data.items(), key = lambda item: item[1])

data[smallest] = my_new_value

1 Comment

Or key=operator.itemgetter(1) if you want to avoid lambdas.

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.