You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何匹配task列表取值并提取hours列表对应位置的最大值

问题需求(中文翻译)

现有两个列表如下:

  • task = [1,1,1,1,2,2,3,4,5,5]
  • hours = [1,7,6,2,3,6,5,2,4,6]

需要实现的功能:

  1. 提示用户输入1到5之间的数字
  2. 匹配task列表中所有和输入值相等的元素对应的索引
  3. 提取hours列表中上述索引位置的所有数值,计算最大值
  4. 最终按格式输出:Task number (用户输入值) can be completed in (计算得到的最大值) hours

示例:用户输入1时,task列表中值为1的元素对应前4个索引,取hours列表前4个元素的最大值7,输出结果为:Task number 1 can be completed in 7 hours


可运行Python实现代码
# 定义给定的两个列表
task = [1,1,1,1,2,2,3,4,5,5]
hours = [1,7,6,2,3,6,5,2,4,6]

# 输入合法性校验
while True:
    try:
        user_input = int(input("请输入1到5之间的数字:"))
        if 1 <= user_input <= 5:
            break
        print("输入不在1-5范围内,请重新输入")
    except ValueError:
        print("输入不是有效整数,请重新输入")

# 收集对应索引的hours数值并计算最大值
target_hours = [hours[i] for i, val in enumerate(task) if val == user_input]
max_hour = max(target_hours)

# 按要求格式输出
print(f"Task number {user_input} can be completed in {max_hour} hours")

运行测试示例

输入:1
输出:Task number 1 can be completed in 7 hours
输入:5
输出:Task number 5 can be completed in 6 hours

内容的提问来源于stack exchange,提问作者No Uce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 20:15:01