gpt4 book ai didi

python - 元组列表中的索引元素

转载 作者:行者123 更新时间:2023-11-28 20:04:42 25 4
gpt4 key购买 nike

我有以下列表,是通过对两个单独的数字列表使用 zip 函数创建的:

[(20, 6),
(21, 4),
(22, 4),
(23, 2),
(24, 8),
(25, 3),
(26, 4),
(27, 4),
(28, 6),
(29, 2),
(30, 8)]

我想知道是否有一种方法可以遍历此列表并在 LHS 上接收与 RHS 上的最大值相对应的数字,即在这种情况下,我想获得 24 和 30,这两个值为 8(RHS 的最大值)。我试过:

## get max of RHS 
max_1 = max([i[1] for i in data])
## for max of LHR, index this location
## on RHS column
number = [i[1] for i in data].index(max_1)

但它不起作用。

最佳答案

之后:

max_1 = max([i[1] for i in data])

尝试:

>>> number = [i[0] for i in data if i[1]==max_1]
>>> number
[24, 30]

关于python - 元组列表中的索引元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35419979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com