简体   繁体   English

在Python中找到最小变量

[英]finding minimum variable in Python

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?我该怎么做?

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.那么,X 是最小变量的索引号(在本例中为 0),可以根据需要使用,或者可以使用 l[X] 来访问变量名称。

(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). (但不要像我一样使用小写的“L”,通常不被认为是好的风格,因为它很容易被误认为是大写的“i”或数字 1)。

Use the python-varname package:使用python-varname包:

https://github.com/pwwang/python-varname 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'

so you have 2 variables v1 and v2 and want to print v1 is small or v2:所以你有 2 个变量 v1 和 v2 并且想要打印 v1 是小或 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.如果您有很多变量,那么将它们存储在字典中可能是一个更好的主意。

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?获取任何变量的名称是一个令人担忧的话题,正如您在如何在 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'
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返回 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 '<')返回 2239429394293(将“>”更改为“<”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM