简体   繁体   English

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

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

Given 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)]

The aim is to find the index of the highest value of the first item in the tuple. 目的是找到元组中第一项的最高值的索引。 With the example input above the output the index of the tuple (-9.89311, -84.10327799999999) 在输入上方的示例输入中,元组的索引(-9.89311, -84.10327799999999)

I have been doing it as such (but it doesn't return the right output): 我一直在这样做(但它不会返回正确的输出):

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

The code returns the index of max(j*j) but is that right? 该代码返回max(j*j)的索引,对吗? Is it different from trying to find max(|j|) ? 与尝试找到max(|j|)什么不同?

But is there another way of achieving the same output? 但是,还有另一种方法可以实现相同的输出吗? Maybe with sorted and reverse and key with some math.abs ? 也许与sortedreverse和一些math.abs key Is the alternative with sorted , reverse and/or key more efficient? sortedreverse和/或key的替代方法是否更有效?

If there's any item in the list of tuples that has the same value, return the first index of the first instance of the maximum absolute value. 如果元组列表中有任何项具有相同的值,则返回最大绝对值的第一个实例的第一个索引。

I'm not sure why you're comparing the value of the first element of the tuple to the index, but this gets you the index of the tuple with the highest absolute value for the first element. 我不确定为什么要将元组的第一个元素的值与索引进行比较,但这会为您获取元组的索引,其中第一个元素的绝对值最高。

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])

Or as a less readable one-liner, 或作为不太易读的单行代码,

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

Here is an alternative, offered without comment on efficiency: 这是一个替代方案,没有对效率进行评论:

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]

would give the index of the last element having the max element in one pass with native function so should be the fastest way 将通过本机函数一次性给出具有max元素的最后一个元素的索引,因此应该是最快的方法

If you really want to have the first of max elements you can do 如果您真的想拥有最大数量的元素,则可以

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

You could do it in 2 steps get the max based on first key in step 1, then get index with the element 您可以分2个步骤进行操作,以第1步中的第一个键为基础获取最大值,然后使用元素获取索引

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

>>> x.index(max_value)

30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM