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

Python如何获取列表最大值索引并提取另一列表对应值

你代码中d[*ll]的写法不符合Python语法,列表不支持通过解包索引一次性取出多个位置的元素。

修改后可运行的代码如下:

d = [1, 2, 3, 4, 5, 6, 7]
g = [1, 2, 3, 100, 4, 5, 100]
m = max(g)
ll = [i for i, j in enumerate(g) if j == m]
# 批量取出d对应索引的元素,转为空格分隔的字符串
res = ' '.join(str(d[i]) for i in ll)
print(f"The longest event time is {m} for the event(s): {res}")

运行后输出结果为:The longest event time is 100 for the event(s): 4 7,完全符合需求。

内容的提问来源于stack exchange,提问作者ACE ONE

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 19:24:04