![](/img/trans.png)
[英]Find the index of a list of tuples with nondistinct values in first index of tuple
[英]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)
我一直在這樣做(但它不會返回正確的輸出):
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
和一些math.abs
key
? 帶sorted
, reverse
和/或key
的替代方法是否更有效?
如果元組列表中有任何項具有相同的值,則返回最大絕對值的第一個實例的第一個索引。
我不確定為什么要將元組的第一個元素的值與索引進行比較,但這會為您獲取元組的索引,其中第一個元素的絕對值最高。
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]))))
這是一個替代方案,沒有對效率進行評論:
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]
將通過本機函數一次性給出具有max元素的最后一個元素的索引,因此應該是最快的方法
如果您真的想擁有最大數量的元素,則可以
print(-max((abs(value[0]),-index) for (index,value) in enumerate(values))[1])
您可以分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.