在Python中查找最小变量 [英] finding minimum variable in Python

查看:107
本文介绍了在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是最小变量的索引号(在这种情况下为0),可以根据需要使用,也可以使用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.

(但不要像我一样使用小写的"L",通常不认为它是好的样式,因为它很容易被误认为是大写的"i"或数字1).

(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).

这篇关于在Python中查找最小变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆