给出一个元组列表:
[(-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)
我一直在这样做(但它没有返回正确的输出):
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
代码返回max(j*j)
的索引,但是这样吗?是否与尝试查找max(|j|)
不同?
但是有另一种方法可以实现相同的输出吗?也许有sorted
和reverse
以及key
有一些math.abs
?替代方案是sorted
,reverse
和/或key
更有效吗?
如果元组列表中的任何项具有相同的值,则返回最大绝对值的第一个实例的第一个索引。
答案 0 :(得分:3)
我不确定为什么要将元组的第一个元素的值与索引进行比较,但是这会得到第一个元素的绝对值最高的元组的索引。
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 :(得分:1)
这是一个替代方案,在没有评论效率的情况下提供:
y = [abs(item[0]) for item in x]
m = max(y)
print(y.index(m))
答案 2 :(得分:1)
print(max((abs(value[0]),index) for (index,value) in enumerate(values))[1]
将给出一次传递中具有max元素的最后一个元素的索引 具有原生功能所以应该是最快的方式
如果你真的想拥有第一个可以做的最大元素
print(-max((abs(value[0]),-index) for (index,value) in enumerate(values))[1])
答案 3 :(得分:0)
你可以分2步完成,根据第1步中的第一个键获取最大值,然后使用元素获取索引
>>> max_value = sorted(x, key=lambda y: y[0])[0]
>>> x.index(max_value)
30