英文:
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 = {"Walk": (2, 5, 0),
"Bike": (4, 18, 3),
"Bus": (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("How many kilometers do you have to travel? "))
print("When you have to travel", distance, "km, taking a", fastest_travel_mode(distance)[0], "will be the quickest way to your destination with an approximate time of", int(fastest_travel_mode(distance)[1]), "minutes.")
if __name__ == "__main__":
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("How many kilometers do you have to travel? "))
mode, time = fastest_travel_mode(distance)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论