鱼C论坛

 找回密码
 立即注册
查看: 1632|回复: 4

代码求助

[复制链接]
发表于 2023-8-12 16:28:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
"""求时间最快和最慢的人与时间"""

times = [0,1,3,3.5,6.5,9.5,10,10.8]
names = ["A","B","C","D","E","F","G"]
x = []




5.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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} 小时。\)
运行这段代码后,你将得到时间最快和时间最慢的人以及他们的时间。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 16:29:25 | 显示全部楼层
你可以使用Python的zip函数将时间和姓名列表合并为一个新的列表,然后使用min和max函数找到最快和最慢的时间。以下是一个示例代码:
times = [0, 1, 3, 3.5, 6.5, 9.5, 10, 10.8]
names = ["A", "B", "C", "D", "E", "F", "G"]

# 将时间和姓名列表合并为一个新的列表
combined = list(zip(times, names))

# 找到最快和最慢的时间
fastest = min(combined)
slowest = max(combined)

print("最快的人和时间:", fastest)
print("最慢的人和时间:", slowest)

输出结果将会是:
最快的人和时间: (0, 'A')
最慢的人和时间: (10.8, 'G')

最快的人是A,时间为0,最慢的人是G,时间为10.8。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 16:29:36 | 显示全部楼层
您可以使用以下代码来查找时间最快和最慢的人以及对应的时间:
times = [0, 1, 3, 3.5, 6.5, 9.5, 10, 10.8]
names = ["A", "B", "C", "D", "E", "F", "G"]

# 找到最快的时间和对应的人
min_time = min(times)
fastest_person = names[times.index(min_time)]

# 找到最慢的时间和对应的人
max_time = max(times)
slowest_person = names[times.index(max_time)]

print("最快的人是:", fastest_person, ",时间:", min_time)
print("最慢的人是:", slowest_person, ",时间:", max_time)

这段代码会输出结果:
最快的人是: A ,时间: 0
最慢的人是: G ,时间: 10.8

这样,您就可以得到时间最快和最慢的人以及对应的时间了。希望能对您有所帮助!如果您有任何其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 19:56:20 | 显示全部楼层
"""求时间最快和最慢的人与时间"""

times = [0,1,3,3.5,6.5,9.5,10,10.8]
names = ["A","B","C","D","E","F","G"]

usetime = []
for nextindex,thistime in enumerate(times,1):
    if nextindex < len(times):
        eachtime = times[nextindex] - thistime
        usetime.append(eachtime)

maxtime = max(usetime)
mintime = min(usetime)
slowperson = []
fastperson = []
for index,eachtime in enumerate(usetime):
    if eachtime == maxtime:
        slowperson.append(names[index])
    if eachtime == mintime:
        fastperson.append(names[index])

print(f"速度最快的是:{fastperson},耗费时间是:{mintime}")
print(f"速度最慢的是:{slowperson},耗费时间是:{maxtime}")
我的回答不是最优的方法,但是能得到正确的输出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 21:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表