【问题标题】:Finding the lowest value in dictionary查找字典中的最小值
【发布时间】:2015-01-16 12:33:03
【问题描述】:

我试图以城市和温度的形式获取用户输入,然后将其保存到字典中。之后它应该打印出最低温度是多少以及它是哪个城市。尽管我的程序从未到达打印消息,但我试图在没有 min 函数的情况下迭代地执行此操作。为什么以及如何解决这个问题?谢谢。

编辑:我知道我可以使用 min 函数使这更容易,但这是一个练习练习,我应该通过迭代找到最小值。另外,如果暂时来说,我会忽略 2 个或更多城市的温度是否最低。

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

【问题讨论】:

    标签: python dictionary min


    【解决方案1】:

    您的代码中的问题是您将lowest 初始化为0。如果您的所有城市都有正温度,那么在您计算最小值时lowest 将永远不会改变,并且任何城市都不会满足if city[key] == lowest: 测试。

    解决此问题的一种方法是将lowest 初始化为城市的温度之一。例如,您可以添加:

    lowest = temperature
    

    下面

    if user == "stop":
    

    lowest初始化为用户最后输入的温度。

    最后,你的代码中的最后一个问题是在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()
    

    【讨论】:

    • 我被告知实际上没有该功能。这是一个练习。
    • 这只能给出一个结果。可能有 2 个或更多城市与最低温度并列
    • 我正在尝试,但不幸的是我遇到了同样的问题,程序不会打印出表明最冷城市及其温度的声明。
    • @user3495234 是的,事实证明您的代码中还有一个错误。请参阅附加说明和更正代码。
    【解决方案2】:

    找到最低点的更简单方法是使用min

    lowest = min(city.values())
    

    “停止”应该 break 退出循环

    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 函数。有人告诉我通过迭代求解最小值。
    • 好的。你这样做的方式应该有效(如果所有温度都大于零)。您需要将温度转换为数字类型。考虑另一种初始化lowest 的方法来处理零以下的温度
    • 不幸的是,程序不会输出“最冷的城市是”消息,所以我有点不知道为什么。
    • 程序做什么做什么。你用 Python3 运行它吗?
    • @gnibbler 我不同意,OP 的解决方案仅在至少一个温度小于零时才有效。否则没有一个城市会击败 lowest 的初始化方式。
    【解决方案3】:

    您可以使用将 lambda 传递给 min 来直接获取键/值。

    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 函数返回的元组的第二个值。

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2015-07-18
      • 2019-02-09
      • 1970-01-01
      • 2013-11-06
      • 2021-07-01
      相关资源
      最近更新 更多