在 Python 中找到最小变量
finding minimum variable in Python
我有一些整数变量,我想找到最小的一个。当我使用:
m1 = min(v1, v2, ...)
我得到最小的值,而不是它的名字。我想知道哪个最小,不知道它的价值!我应该怎么做?
所以你有 2 个变量 v1 和 v2 并且想要打印 v1 is small or v2:
if( v1 > v2 ):
print "v1 =": str(v1)
#or print "v1 is smaller"
else:
print "v2 =": str(v2)
如果您有很多变量,那么将它们存储在字典中可能是个更好的主意。
如果索引号有效,您可以这样做:
# 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就是最小变量的索引号(本例中为0),可以根据需要使用,也可以使用l[X]来访问变量名。
(但不要像我一样使用小写 "L",通常不被认为是好的风格,因为它很容易被误认为是大写 "i" 或数字 1)。
如您在 How to get a variable name as a string in Python?
中所见,获取任何变量的名称都是一个令人担忧的话题
但是,如果上述答案中的一个解决方案是可以接受的,那么您就有了一个包含变量 name/value 对的字典,您可以对其进行排序并取最小值。例如:
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
现在,要为其设置一个变量,只需输入:
variable = ShowMinValue(listOfPossibleNumbers)
如果你想要一个永不例外的版本:
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 '<')
使用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'
我有一些整数变量,我想找到最小的一个。当我使用:
m1 = min(v1, v2, ...)
我得到最小的值,而不是它的名字。我想知道哪个最小,不知道它的价值!我应该怎么做?
所以你有 2 个变量 v1 和 v2 并且想要打印 v1 is small or v2:
if( v1 > v2 ):
print "v1 =": str(v1)
#or print "v1 is smaller"
else:
print "v2 =": str(v2)
如果您有很多变量,那么将它们存储在字典中可能是个更好的主意。
如果索引号有效,您可以这样做:
# 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就是最小变量的索引号(本例中为0),可以根据需要使用,也可以使用l[X]来访问变量名。
(但不要像我一样使用小写 "L",通常不被认为是好的风格,因为它很容易被误认为是大写 "i" 或数字 1)。
如您在 How to get a variable name as a string in Python?
中所见,获取任何变量的名称都是一个令人担忧的话题但是,如果上述答案中的一个解决方案是可以接受的,那么您就有了一个包含变量 name/value 对的字典,您可以对其进行排序并取最小值。例如:
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
现在,要为其设置一个变量,只需输入:
variable = ShowMinValue(listOfPossibleNumbers)
如果你想要一个永不例外的版本:
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 '<')
使用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'