|
发表于 2023-8-12 16:29:09
|
显示全部楼层
求助:寻找列表中时间最快和最慢的人与时间
问题描述: 我有一个包含时间和姓名的列表,我想找出时间最快和时间最慢的人以及他们的时间。
- times = [0, 1, 3, 3.5, 6.5, 9.5, 10, 10.8]
- names = [\A\, \B\, \C\, \D\, \E\, \F\, \G\]
- x = []
复制代码
回答: 你可以使用Python代码来找出时间最快和时间最慢的人以及他们的时间。以下是修改后的完整代码:
- times = [0, 1, 3, 3.5, 6.5, 9.5, 10, 10.8]
- names = [\A\, \B\, \C\, \D\, \E\, \F\, \G\]
- # 找出最快和最慢的时间及其对应的索引
- fastest_time = min(times)
- slowest_time = max(times)
- fastest_index = times.index(fastest_time)
- slowest_index = times.index(slowest_time)
- # 根据索引找到对应的姓名
- fastest_person = names[fastest_index]
- slowest_person = names[slowest_index]
- # 打印结果
- print(f\时间最快的人是 {fastest_person},时间为 {fastest_time} 小时。\)
- print(f\时间最慢的人是 {slowest_person},时间为 {slowest_time} 小时。\)
复制代码
运行这段代码后,你将得到时间最快和时间最慢的人以及他们的时间。 |
|