如何在Python中查找列表中最大值的索引

2,382 阅读11分钟

python中的列表是最常用的数据结构之一。在这篇文章中,我们将讨论在Python中寻找列表中最大值的索引的不同方法。

目录

  1. 在Python中使用for循环查找列表中最大值的索引
  2. 使用max()函数和index()方法查找列表中最大值的索引
    1. max()函数
    2. index()方法
  3. 使用max()函数和for循环在多处出现的情况下查找最大值的索引
  4. 使用max()函数和列表理解,在多次出现的情况下查找最大值的索引
  5. 使用max()和enumerate()函数在多次出现的情况下查找最大值的索引
  6. 使用Numpy模块在Python中查找列表中最大值的索引
  7. 总结

在Python中使用for循环查找列表中最大值的索引

为了使用for循环找到列表中最大值的索引,我们将使用以下程序。

  • 首先,我们将初始化一个变量max_index 为0,假设第一个元素是列表中的最大元素。
  • 之后,我们将使用len() 函数找到列表的长度。len() 函数将一个列表作为其输入参数,并返回该列表的长度。
  • 一旦我们得到列表的长度,我们将使用range() 函数和for循环来迭代列表。在迭代过程中,在列表的每个索引处,我们将检查当前索引处的元素是否大于索引处的元素max_index
  • 如果我们发现一个索引的元素大于索引max_index 的元素,我们将把当前索引分配给变量max_index

在对整个列表进行迭代后,我们将在变量max_index 中得到最大元素的索引。

你可以在下面的例子中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 21, 12]
print("The list is:", myList)
max_index = 0
list_len = len(myList)
for index in range(list_len):
    if myList[index] > myList[max_index]:
        max_index = index
print("Index of the maximum element is:", max_index)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 21, 12]
Index of the maximum element is: 7

在上面的例子中,如果有多个最大元素出现,我们得到最大元素的最左边的索引。

为了得到最大元素的最右边的索引,你可以在比较元素的时候使用大于或等于(>=) ,而不是大于(>) ,如下图所示。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_index = 0
list_len = len(myList)
for index in range(list_len):
    if myList[index] >= myList[max_index]:
        max_index = index
print("Index of the maximum element is:", max_index)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of the maximum element is: 9

使用max()函数和index()方法查找列表中最大值的索引

为了编写更多的pythonic代码,你可以使用max() 函数和index() 方法来查找python中列表中最大值的索引。

max()函数

max() 函数接收一个容器对象,如列表、元组或集合作为其输入参数。执行后,它返回容器对象的最大元素。

例如,如果我们给一个列表作为max() 函数的输入参数,它将返回该列表的最大元素。你可以在下面的例子中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
print("The maximum value is:", max_val)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
The maximum value is: 55

index()方法

index() 方法,当在一个列表上调用时,将一个元素作为其输入参数,并返回该元素从列表开始第一次出现的索引。你可以在下面的例子中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
index = myList.index(23)
print("Index of 23 is:", index)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of 23 is: 2

当输入的元素不在列表中时,index() 方法会引发一个ValueError 异常,说给定的元素不在列表中,如下所示。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
index = myList.index(112)
print("Index of 112 is:", index)

输出:当输入的元素在列表中不存在时, 方法会产生一个异常,说给定的元素在列表中不存在,如下图。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 3, in <module>
    index = myList.index(112)
ValueError: 112 is not in list

要在 python 中找到列表中最大值的索引,我们将首先使用max() 函数找到列表中的最大元素。之后,我们将在列表中调用index() 方法,以最大元素作为其输入参数。执行index() 方法后,我们将得到列表中最大元素的索引。你可以在下面的例子中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
index = myList.index(max_val)
print("Index of maximum value is:", index)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of maximum value is: 7

这里,最大值55存在于两个索引中。然而,index() 方法只返回最左边出现的元素的索引。

使用max()函数和For Loop在多处出现的情况下寻找最大值的索引

在使用max() 函数和index() 方法的方法中,我们只能在python中找到列表中最大值的第一个索引。为了在最大值多次出现的情况下找到列表中最大值的索引,我们可以使用下面的方法。

  • 首先,我们将使用max() 函数找到列表中的最大值。
  • 然后,我们将创建一个名为list_indices 的列表来存储列表中的最大值的索引。
  • 之后,我们将使用len() 函数找到输入列表的长度。 len() 函数将列表作为其输入参数,并返回列表的长度。我们将把长度存储在一个变量list_len
  • 获得列表的长度后,我们将使用range() 函数创建一个从0到list_len 的数字序列。range() 函数将list_len 作为其输入参数,并返回一个包含从 0 到list_len-1 的数字序列。
  • 现在,我们将使用for循环来迭代这个数字序列。在迭代过程中,我们将检查列表中与序列中的当前数字相同的索引处的元素是否等于最大值。
  • 如果我们在序列中找到一个索引,该索引在列表中存在最大值。我们将使用append() 方法将该索引存储在list_indicesappend() 方法,当在列表上调用时,将把索引值作为其输入参数,并将其添加到列表中。

在执行for循环后,我们将在list_indices 中得到所有最大值的索引。 你可以在下面的例子中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
list_indices = []
list_len = len(myList)
sequence = range(list_len)
for index in sequence:
    if myList[index] == max_val:
        list_indices.append(index)
print("Indices of maximum element are:", list_indices)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]

使用max()函数和列表理解在多次出现的情况下寻找最大值的索引

你可以不使用for循环,而是使用列表理解与range() 函数和max() 函数一起从一个给定的输入列表中获得最大值的索引列表,如下图所示。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
list_len = len(myList)
sequence = range(list_len)
list_indices = [index for index in sequence if myList[index] == max_val]
print("Indices of maximum element are:", list_indices)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]

使用max()和enumerate()函数在多处出现的情况下寻找最大值的索引

enumerate() 函数是用来给容器对象的元素分配一个计数器的。它接收一个像列表一样的容器对象,并返回一个图元的列表。在图元列表中,每个图元都包含一个表示元素索引的数字作为它的第一个元素,而列表中的相应值作为它的第二个输入参数。你可以在下面的例子中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
print("Enumerated list is:", enumerated_list)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Enumerated list is: [(0, 1), (1, 2), (2, 23), (3, 32), (4, 12), (5, 44), (6, 34), (7, 55), (8, 46), (9, 55), (10, 21), (11, 12)]

为了使用enumerate() 函数找到最大元素的索引,我们将使用以下步骤。

  • 首先,我们将使用max() 函数找到输入列表中的最大元素。
  • 然后,我们将创建一个名为max_indices 的空列表来存储列表中最大元素的索引。
  • 在创建列表后,我们将使用enumerate() 函数获得列举列表中的图元。
  • 一旦我们得到了枚举列表,我们将使用for循环来迭代这些图元。
  • 迭代时,我们将检查元组中的当前值是否等于最大值。如果是,我们将把与元组中的元素相关的索引存储在max_indices 。为此,我们将使用append() 方法,将当前的索引作为其输入参数调用到max_indices
  • 如果元组中的当前值不等于最大值,我们将移动到下一个元组。

执行for循环后,我们将得到max_indices 列表中所有最大元素的索引。你可以在下面的代码中观察到这一点。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
max_indices = []
max_element = max(myList)
for element_tuple in enumerated_list:
    index = element_tuple[0]
    element = element_tuple[1]
    if element == max_element:
        max_indices.append(index)
print("Indices of maximum element are:", max_indices)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]

在上面的例子中没有使用for循环,你可以使用列表理解和enumerate() 函数来找到列表中最大元素的索引,如下所示。

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
max_element = max(myList)
max_indices = [index for (index, element) in enumerated_list if element == max_element]
print("Indices of maximum element are:", max_indices)

输出结果。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]

使用Numpy模块在Python中查找列表中最大值的索引

我们也可以使用numpy模块在Python中找到列表中最大值的索引。numpy模块为我们提供了argmax() 方法来查找列表中最大值的索引。argmax() 方法,当在 numpy 数组上调用时,返回最大元素的索引。

为了在 python 中找到列表中的最大值,我们将首先使用array() 构造函数将我们的输入列表转换成一个 numpy 数组。array() 构造函数接受一个像列表一样的容器对象作为其输入参数。执行后,它返回一个包含与输入容器对象相同数量元素的 numpy 数组。

从输入的列表中创建numpy数组后,我们将使用argmax() 函数来查找列表中最大值的索引,如下所示。

import numpy

myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
array = numpy.array(myList)
max_index = array.argmax()
print("Index of maximum element is:", max_index)

输出。

The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of maximum element is: 7

如果最大值有多个出现,argmax() 方法将返回列表左边第一个出现的最大值的索引。你可以在上面的例子中观察到这一点。

总结

在这篇文章中,我们讨论了在python中查找列表中最大值的索引的不同方法。如果你只需要找到第一次出现的最大值的索引,你可以使用max() 函数和index() 方法。如果你需要获得最大值出现的所有索引,你应该使用max() 函数和enumerate() 函数的方法。

我希望你喜欢阅读这篇文章。要了解更多关于Python编程的信息,你可以阅读这篇文章:如何在Python中删除一个列表中出现的所有字符。你可能也喜欢这篇文章:如何检查一个Python字符串是否包含一个数字

请继续关注更多内容丰富的文章。

学习愉快!

The postFind the Index of Max Value in a List in Pythonappeared first onPythonForBeginners.com.