如何在元组列表中找到第一次的最大绝对索引?

How to find the index of the maximum absolute of the first time in a list of tuples?

给定一个元组列表:

[(-8.33958, -84.01769099999999), (-7.96129, -84.37399199999999), (-8.33958, -83.84126699999999), (-7.96129, -84.19756499999998), (-7.24097, -85.581651), (-6.86267, -85.937952), (-7.24097, -85.405227), (-6.86267, -85.76152499999999), (-9.4382, -83.473767), (-9.4382, -83.473767), (-8.35625, -85.11197399999999), (-9.0599, -83.83006499999999), (-9.0599, -83.83006499999999), (-7.97795, -85.46824499999998), (-8.35625, -84.93524699999999), (-7.97795, -85.29151799999998), (-8.33958, -85.03772699999999), (-8.00311, -88.17046199999997), (-5.88285, -86.59070369999999), (-6.86267, -88.655385), (-9.37174, -86.88101999999999), (-7.34506, -88.24291199999999), (-8.22317, -87.13259099999999), (-7.72054, -86.124309), (-8.35625, -86.131707), (-8.35625, -86.131707), (-7.03703, -88.90182), (-8.51394, -86.422992), (-8.51394, -85.83968999999999), (-9.61255, -85.510092), (-9.89311, -84.10327799999999), (-7.96129, -87.540312), (-9.13791, -86.022645)]

目的是找到元组中第一项的最大值的索引。使用上面的示例输入输出元组的索引 (-9.89311, -84.10327799999999)

我一直这样做(但它没有 return 正确的输出):

x = [(-8.33958, -84.01769099999999), (-7.96129, -84.37399199999999), (-8.33958, -83.84126699999999), (-7.96129, -84.19756499999998), (-7.24097, -85.581651), (-6.86267, -85.937952), (-7.24097, -85.405227), (-6.86267, -85.76152499999999), (-9.4382, -83.473767), (-9.4382, -83.473767), (-8.35625, -85.11197399999999), (-9.0599, -83.83006499999999), (-9.0599, -83.83006499999999), (-7.97795, -85.46824499999998), (-8.35625, -84.93524699999999), (-7.97795, -85.29151799999998), (-8.33958, -85.03772699999999), (-8.00311, -88.17046199999997), (-5.88285, -86.59070369999999), (-6.86267, -88.655385), (-9.37174, -86.88101999999999), (-7.34506, -88.24291199999999), (-8.22317, -87.13259099999999), (-7.72054, -86.124309), (-8.35625, -86.131707), (-8.35625, -86.131707), (-7.03703, -88.90182), (-8.51394, -86.422992), (-8.51394, -85.83968999999999), (-9.61255, -85.510092), (-9.89311, -84.10327799999999), (-7.96129, -87.540312), (-9.13791, -86.022645)]


index_of_max_abs_j = -1
for i, (j,k) in enumerate(x):
    if j*j > index_of_max_abs_j:
        index_of_max_abs_j = i
print index_of_max_abs_j

代码 return 是 max(j*j) 的索引,但对吗?它与尝试查找 max(|j|) 不同吗?

但是还有另一种方法可以实现相同的输出吗? 也许 sortedreverse 以及 key 和一些 math.abssortedreverse and/or key 的替代方案是否更有效?

如果元组列表中有任何项具有相同的值,return 最大绝对值的第一个实例的第一个索引。

我不确定您为什么要将元组第一个元素的值与索引进行比较,但这会为您提供第一个元素绝对值最高的元组索引。

max_index = -1
max_value = 0
for i, z in enumerate(x):
    value = abs(z[0])
    if value > max_value:
        max_index = i
        max_value = value

print(x[max_index])

或者作为一种可读性较差的单行,

print(x.index(max(x, key=lambda y:abs(y[0]))))

您可以分两步完成,根据第 1 步中的第一个键获取最大值,然后使用元素获取索引

>>> max_value = sorted(x, key=lambda y: y[0])[0]

>>> x.index(max_value)

30

这是一个替代方案,提供时没有对效率发表评论:

y = [abs(item[0]) for item in x]
m = max(y)
print(y.index(m))
print(max((abs(value[0]),index) for (index,value) in enumerate(values))[1]

将在一次传递中给出具有最大元素的最后一个元素的索引 具有本机功能,所以应该是最快的方法

如果你真的想拥有最大元素中的第一个,你可以这样做

print(-max((abs(value[0]),-index) for (index,value) in enumerate(values))[1])