Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打印出最短的人名而不是最短的身高?(Python) def高度(h1、h2、h3): 如果h1_Python - Fatal编程技术网

如何打印出最短的人名而不是最短的身高?(Python) def高度(h1、h2、h3): 如果h1

如何打印出最短的人名而不是最短的身高?(Python) def高度(h1、h2、h3): 如果h1,python,Python,您不能(轻松地)使用变量名作为数据。使用dict存储名称到高度的映射 def height(h1,h2,h3): if h1 <= h2 and h1 <= h3: return h1 elif h2 <= h1 and h2 <= h3: return h2 else: return h3 result = height(175, 174, 178) print(result) 然后你就可以写了

您不能(轻松地)使用变量名作为数据。使用dict存储名称到高度的映射

def height(h1,h2,h3):
    if h1 <= h2 and h1 <= h3:
        return h1
    elif h2 <= h1 and h2 <= h3:
        return h2
    else:
        return h3
result = height(175, 174, 178)
print(result)
然后你就可以写了

>>> people_heights = {"A": 175, "B": 174, "C": 178}
min
取任意iterable;
dict
迭代器产生其键。
min
key
参数是一个应用于iterable的每个元素以获取其“大小”的函数,而绑定方法
people\u heights.get
正是您需要返回的可调用项,例如
178
用于
'C'


调整现有的
高度
功能可能类似于

>>> min(people_heights, key=people_heights.get)
'B'

我们在这里看不到任何关于person或name的内容,您是否做了其他事情?如果您只定义变量
A
B
C
A=175
,并期望函数在调用
height(A,B,C)
时返回变量的名称,则没有正常的方法。人名A=175cm,B=174cm,C=178厘米。我想在我的代码中包括这一点。我如何包括这一点,并打印出最短的人名,而不是最短的身高?
def height(d1, d2, d3):
    n1, h1 = d1
    n2, h2 = d2
    n3, h3 = d3
    if h1 <= h2 and h1 <= h3:
        return n1
    elif h2 <= h1 and h2 <= h3:
        return n2
    else:
        return n3
height(*people_heights.items())