简体   繁体   English

如何找到 Python 列表中最大值的索引?

[英]How to find the index of the max value in a list for Python?

I have a list, and I need to find the maximum element in the list and also record the index at which that max element is at.我有一个列表,我需要找到列表中的最大元素,并记录该最大元素所在的索引。

This is my code.这是我的代码。

list_c = [-14, 7, -9, 2]

max_val = 0
idx_max = 0

for i in range(len(list_c)):
    if list_c[i] > max_val:
        max_val = list_c[i]
        idx_max = list_c.index(i)

return list_c, max_val, idx_max

Please help.请帮忙。 I am just beginning to code and got stuck here.我刚刚开始编码并被困在这里。

.index can only be done on elements in the list. .index只能对列表中的元素进行。 So, do idx_max = list_c.index(list_c[i])所以,做idx_max = list_c.index(list_c[i])

max_val = list_c[i]
idx_max = list_c.index(list_c[i])

However, here is a shortened version然而,这是一个缩短的版本

list_c = [-14, 7, -9, 2]
print(max(list_c),list_c.index(max(list_c)))
  • You are trying to find the index of i .您正在尝试查找i的索引。 It should be list_c[i] .它应该是list_c[i]

Easy way/Better way is: idx_max = i .简单的方法/更好的方法是: idx_max = i (Based on @Matthias comment above.) (基于上面的@Matthias 评论。)

  • Use print to print the results and not return .使用print打印结果而不return You use return inside functions.您在函数内部使用return
  • Also your code doesn't work if list_c has all negative values because you are setting max_val to 0 .如果list_c具有所有负值,您的代码也不起作用,因为您将max_val设置为0 You get a 0 always.你总是得到 0。
list_c = [-14, 7, -9, 2]

max_val = 0
idx_max = 0

for i in range(len(list_c)):
    if list_c[i] > max_val:
        max_val = list_c[i]
        idx_max = i

print(list_c, max_val, idx_max)

[-14, 7, -9, 2] 7 1

It is easiest to just apply logic to the entire list.将逻辑应用于整个列表是最简单的。 You can get the max value of a list using max(list), and you can use the same index function you use above to get the index of this number in the list.您可以使用 max(list) 获取列表的最大值,并且您可以使用与上面使用的相同的索引函数来获取该数字在列表中的索引。

max_val = max(list_c)
idx_max = list_c.index(max_val)

If you want to loop over the values to find the max, then take note of the following:如果要遍历值以找到最大值,请注意以下几点:

  1. list.index(value) will find the index of that value in the list. list.index(value) 将在列表中找到该值的索引。 In your case you are writing list.index(index_number) which doesnt make sense.在您的情况下,您正在编写没有意义的 list.index(index_number) 。 'i' already represents the list index, so just set idx_max = 'i'. 'i' 已经代表列表索引,所以只需设置 idx_max = 'i'。

  2. Further, you should note that if you set max_val to zero, if all the list items are less than zero, it will stay at zero during the loop and never find the max value.此外,您应该注意,如果您将 max_val 设置为零,如果所有列表项都小于零,它将在循环期间保持为零并且永远不会找到最大值。 It is better to start by setting max_val to the first item of the list ie list_c[0].最好先将 max_val 设置为列表的第一项,即 list_c[0]。

     list_c = [-14, 7, -9, 2] max_val = list_c[0] idx_max = 0 for i in range(len(list_c)): if list_c[i] > max_val: max_val = list_c[i] idx_max = i

NB 'return' is used to return values from a function. NB 'return' 用于从函数返回值。 If you want to print the values, use print(list_c, max_val, idx_max).如果要打印值,请使用 print(list_c, max_val, idx_max)。

You can use NumPy argmax() by converting your list to a NumPy array.您可以通过将列表转换为 NumPy 数组来使用 NumPy argmax()。

import numpy as np
list_c = [-14, 7, -9, 2]
print("Index max:",np.argmax(np.asarray(list_c)))
print("Value max:",list_c[np.argmax(np.asarray(list_c))])

Index max: 1索引最大值:1

Value max: 7最大值:7

Looping twice through an array is inefficient.遍历数组两次是低效的。 Use built-in enumerate() and max() with the comparison lambda expression returning the second element of the enumeration:使用内置的enumerate()max()与比较 lambda 表达式返回枚举的第二个元素:

>>> list_c = [-14, 7, -9, 2]
>>> print(max(enumerate(list_c), key=lambda x: x[1]))
(1, 7)

All you need is .max() and .index() function.您只需要 .max() 和 .index() 函数。 First, you find max element of the list, then just simply get it index using index() function.首先,您找到列表的最大元素,然后使用 index() 函数简单地获取它的索引。

You can see a complete tutorial here: https://www.kite.com/python/answers/how-to-find-the-index-of-the-max-value-in-a-list-in-python你可以在这里看到一个完整的教程: https : //www.kite.com/python/answers/how-to-find-the-index-of-the-max-value-in-a-list-in-python

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

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