1

I have some integer variables and I want to find smallest one. When I use:

m1 = min(v1, v2, ...)

I get the value of the smallest one, not its name. I want to know which one is smallest, not know it's value! How should I do this?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Mehdi Abbassi
  • 627
  • 1
  • 7
  • 24
  • 1
    Suppose `v2` is the smallest. Do you want `m1` to equal *the string* `'v2'`? or do you want it to equal the index 1 (since Python uses 0-based indexing and assuming the `v`s are all numbered)? – unutbu Jul 09 '15 at 21:33
  • 1
    I suspect he wants `m1` to be a reference to the same mutable object to which `v2` refers. Of course, he can't have that. – Robᵩ Jul 09 '15 at 21:34
  • Yes, I want v2. If not possible, the index (2) would help me. :) – Mehdi Abbassi Jul 09 '15 at 21:45
  • possible duplicate of [Getting the name of a variable as a string](http://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string) – John Ruddell Jul 09 '15 at 21:47

5 Answers5

2

If the index number will work, you could do this:

# enter variables
a = 1
b = 2
c = 3

# place variables in list
l = (a,b,c)

# get index of smallest item in list
X = l.index(min(l))

# to print the name of the variable
print(l[X])

X, then, is the index number of the smallest variable (in this case, 0) and can be used as needed, or l[X] could be used to access the variable name.

(But don't use lower case "L" like I did, not usually considered good style 'cuz it can easily be mistaken for upper cas "i" or the number 1).

THR33
  • 44
  • 3
2

Getting the name of any variable is a fraught topic as you can see in How to get a variable name as a string in Python?

However, If one of the solutions in the above answer is acceptable, then you have a dictionary of variable name/value pairs, which you can sort and take the minimum. For example:

vals = {"V1": 1, "V2": 3, "V3": 0, "V4": 7}
sorted(vals.items(), key=lambda t: t[1])[0][0]
>>> 'V3'
Community
  • 1
  • 1
Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
1

Use the python-varname package:

https://github.com/pwwang/python-varname

from varname.helpers import Wrapper

v1 = Wrapper(3)
v2 = Wrapper(2)
v3 = Wrapper(5)

v = min(v1, v2, v3, key=lambda x:x.value)

assert v is v2

print(v.name)
# 'v2'

Panwen Wang
  • 3,573
  • 1
  • 18
  • 39
0

so you have 2 variables v1 and v2 and want to print v1 is small or v2:

if( v1 > v2 ):
    print "v1 =": str(v1)
    #or print "v1 is smaller"  
else:
    print "v2 =": str(v2)

if you have a lot of variables then storing them in a dictionary might be a better idea.

Deepak
  • 1,503
  • 3
  • 22
  • 45
0
def ShowMinValue(listofvalues):
     x = float(listofvalues[0])
     for i in range(len(listofvalues)):
         if x > float(listofvalues[i]):
            x = float(listofvalues[i])
     return x
print ShowMinValue([5,'0.1',6,4,3,7,4,1,234,'2239429394293',234656])

returns 0.1

now, to set a variable to it, just put:

variable = ShowMinValue(listOfPossibleNumbers)

of if you want a never-exception version:

def ShowMinValue(listofvalues):
    try:
        x = createdMaxDef(listofnumbers) #Your maximum possible number, or create an max() def to set it. to make it, set that '>' to '<' and rename the method
    except Exception:
        pass
    for i in range(len(listofvalues)):
        try:
            if x > float(listofvalues[i]):
                x = float(listofvalues[i])
        except Exception:
            pass
     return x
print ShowMinValue([5,'0.1',6,4,'',3,7,4,1,234,'2239429394293',234656])

returns 2239429394293 ( changing the '>' to '<')

xNecr00x
  • 29
  • 3