Python 在字典中查找最低值
我试图以城市和温度的形式获取用户输入,然后将其保存到字典中。在那之后,它应该打印出最低温度是多少以及它是哪个城市。我尝试在没有min函数的情况下迭代执行此操作,尽管我的程序从未到达打印消息。为什么以及如何解决这个问题?多谢各位 编辑:我知道我可以使用min函数来简化这一过程,但这是一个练习,我应该通过迭代找到最小值。另外,如果暂时来说,我忽略了2个或更多城市的最低温度Python 在字典中查找最低值,python,dictionary,min,Python,Dictionary,Min,我试图以城市和温度的形式获取用户输入,然后将其保存到字典中。在那之后,它应该打印出最低温度是多少以及它是哪个城市。我尝试在没有min函数的情况下迭代执行此操作,尽管我的程序从未到达打印消息。为什么以及如何解决这个问题?多谢各位 编辑:我知道我可以使用min函数来简化这一过程,但这是一个练习,我应该通过迭代找到最小值。另外,如果暂时来说,我忽略了2个或更多城市的最低温度 def main(): city = {} lowest = 0 while True: user = input("E
def main():
city = {}
lowest = 0
while True:
user = input("Enter city followed by temperature: ")
if user == "stop":
print(city)
for value in city.values():
if int(value) < lowest:
lowest = int(value)
for key in city:
if city[key] == lowest:
print("The coldest city is,", city[key])
break
name, temperature = user.split()
city[name] = temperature
main()
def main():
城市={}
最低=0
尽管如此:
用户=输入(“输入城市,然后输入温度:”)
如果用户==“停止”:
印刷品(城市)
对于城市中的值。值():
如果int(值)<最低值:
最低=整数(值)
对于关键城市:
如果城市[钥匙]==最低:
打印(“最冷的城市是,”,城市[key])
打破
名称,温度=user.split()
城市[名称]=温度
main()
代码中的问题是您将最低值初始化为0
。如果您的所有城市都有正温度,最低值
在计算最低值时不会改变,并且如果城市[key]==最低值:
测试,任何城市都不会满足
解决此问题的一种方法是将最低值初始化为城市的一个温度。例如,您可以添加:
lowest = temperature
下面
将最低值
初始化为用户最后一次输入的温度
最后,代码中的最后一个问题是在if city[key]==lowest
测试中:构建字典的方式,city[key]
是一个字符串,而lowest
是一个整数,它们永远不会相等。我建议在构建字典时将温度转换为整数,这样以后就不必处理转换。整体更正代码如下所示:
def main():
city = {}
while True:
user = input("Enter city followed by temperature: ")
if user == "stop":
print(city)
lowest = temperature
for value in city.values():
if value < lowest:
lowest = value
for key in city:
if city[key] == lowest:
print("The coldest city is,", city)
break
name, temperature = user.split()
temperature = int(temperature)
city[name] = temperature
main()
def main():
城市={}
尽管如此:
用户=输入(“输入城市,然后输入温度:”)
如果用户==“停止”:
印刷品(城市)
最低=温度
对于城市中的值。值():
如果值<最低值:
最低=值
对于关键城市:
如果城市[钥匙]==最低:
打印(“最冷的城市是,”,城市)
打破
名称,温度=user.split()
温度=int(温度)
城市[名称]=温度
main()
找到最低值的一个更简单的方法是使用min
lowest = min(city.values())
“停止”应该中断你的循环
while True:
user = input("Enter city followed by temperature: ")
if user == "stop":
break
name, temperature = user.split()
city[name] = float(temperature) # convert to float (or perhaps int)
print(city)
lowest = min(city.values())
for key in city:
if city[key] == lowest:
print("The coldest city is,", city[key])
<>你可能想考虑当有一个以上城市的最低温度 时打印不同的消息。你可以使用λ到min直接获得键/值。< /P>
while True:
user = input("Enter city followed by temperature: ")
if user == "stop":
break
name, temperature = user.split()
city[name] = float(temperature) # convert to float (or perhaps int)
lowest_city, lowest_temp = min(city.iteritems(), key=lambda x:x[1])
print("The coldest city is, %s, temperature = %.3f" % (lowest_city, lowest_temp))
这里lambda函数告诉min函数根据
iteritems函数返回的元组的第二个值。我被告知在不使用该函数的情况下执行此操作。这是一个练习,只能给出一个结果。可能有两个或多个城市的最低温度相同。我正在尝试,但不幸的是,我遇到了同样的问题,程序无法打印出指示最冷城市及其温度的语句。@user3495234是的,您的代码中还有一个错误。请参阅附加说明和更正的代码。我实际上不允许使用min函数。我被告知通过迭代求最小值。好的。你这样做应该是可行的(如果所有的温度都大于零)。不过,您需要将温度转换为数字类型。考虑一种初始化<代码>最低< /代码>以处理低于Ze0WELL的温度的方法。不幸的是,程序不会输出“最冷的城市是”消息,所以我有点不明白为什么。程序是做什么的。你是用Python3运行的吗?@gnibbler我不同意,OP的解决方案只有在至少一个温度低于零时才有效。否则,没有一个城市会超过lower
初始化的方式。
while True:
user = input("Enter city followed by temperature: ")
if user == "stop":
break
name, temperature = user.split()
city[name] = float(temperature) # convert to float (or perhaps int)
lowest_city, lowest_temp = min(city.iteritems(), key=lambda x:x[1])
print("The coldest city is, %s, temperature = %.3f" % (lowest_city, lowest_temp))