简体   繁体   English

如何找到列表的特定元素在列表中的距离? 换句话说,特定元素在结果列表中的位置

[英]How do I find how far in the list a specific element of the list is? In other words, the location of a specific element in a list of results

I have a function that ends up putting the results on the list.我有一个 function 最终将结果放在列表中。 I then use max(my_list) to give me the maximum of that list.然后我使用 max(my_list) 给我该列表的最大值。 It gives 12. But now I want to now how far into this list the maximum lies.它给出了 12。但现在我想知道这个列表中的最大谎言有多远。 So I can find the corresponding element of another list my_angles所以我可以找到另一个列表 my_angles 的对应元素

You can either use the built-in functions or numpy.您可以使用内置函数或 numpy。

Built-in functions内置函数

my_list = [0, 5, 4, 9, -2, 6]
max_value = max(my_list)
idx = my_list.index(max_value)
print(max_value) # 9
print(idx) # 3

Numpy Numpy

import numpy as np

my_list = [0, 5, 4, 9, -2, 6]
idx = np.argmax(my_list)
max_value = my_list[idx]
print(max_value) # 9
print(idx) # 3

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

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