for循环查找Python字典中的最小值?

huangapple go评论67阅读模式
英文:

How to: for loop minimum value dictionary python?

问题

以下是已经翻译好的代码部分:

def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    for fastest_mode in sorted(travel_time.items(), key=lambda kv: kv[1]):
        return fastest_mode

你可以尝试使用以下代码,包括使用循环来解包字典、查找字典中的最小值,并返回它:

def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    min_mode = None
    min_time = float('inf')
    for mode, time in travel_time.items():
        if time < min_time:
            min_mode = mode
            min_time = time
    return (min_mode, min_time)

这段代码会遍历字典中的每个模式和对应的时间,找到最小的时间,并返回一个包含最快模式和时间的元组。

英文:

I have a problem with creating a tuple with a minimum value from a dictionary. I found a way with the lambda function but my teacher says that I need to create a for loop where i "unpack" the dictionary, find the minimum value of the dictionary, and return it.

I'm lost, this is what I have now but I need to change the fastest_travel_mode(distance):

travel_times = {&quot;Walk&quot;: (2, 5, 0),
                &quot;Bike&quot;: (4, 18, 3),
                &quot;Bus&quot;: (8, 35, 8)}


def travel_time_per_mode(distance):
    fastest_times = {}
    for mode in travel_times:
        setup_time, speed, finish_time = travel_times[mode]
        fastest_times[mode] = setup_time + 60 / speed * distance + finish_time
    return fastest_times


def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    for fastest_mode in sorted(travel_time.items(), key=lambda kv: kv[1]):
        return fastest_mode


def main():
    distance = float(input(&quot;How many kilometers do you have to travel? &quot;))
    print(&quot;When you have to travel&quot;, distance, &quot;km, taking a&quot;, fastest_travel_mode(distance)[0], &quot;will be the quickest way to your destination with an approximate time of&quot;, int(fastest_travel_mode(distance)[1]), &quot;minutes.&quot;)


if __name__ == &quot;__main__&quot;:
    main()




I tried:

def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    for fastest_mode in sorted(travel_time.items(), key=lambda kv: kv[1]):
        return fastest_mode

It works but i need to do it another way including the steps creating a for loop "unpack" the dictionary, find the minimum value of the dictionary, and return it.

答案1

得分: 0

你可以使用 min 方法。默认情况下,它通过查看 dict 键来搜索最小值,但你可以提供 key 参数来搜索其他内容。在这种情况下,我们使用 get 函数,该函数告诉 找到具有最小值的键

def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    mode = min(travel_time, key=travel_time.get)
    return mode, travel_time[mode]

现在你进行了两次计算,可以只计算一次并存储结果:

def main():
    distance = float(input("你要旅行多少公里? "))
    mode, time = fastest_travel_mode(distance)
    ...
英文:

You can use min method. By default it searches minimum by looking at dict keys, but you can provide key argument to search by something else. In that case we use get function which tells find the key with minimum value

def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    mode = min(travel_time, key=travel_time.get)
    return mode, travel_time[mode]

And now you do the calculation twice, you can do it once and store the result:

def main():
    distance = float(input(&quot;How many kilometers do you have to travel? &quot;))
    mode, time = fastest_travel_mode(distance)
    ...

huangapple
  • 本文由 发表于 2023年2月10日 07:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定