Python 查找矩阵每行的最大元素
在这篇文章中,我们将学习一个使用Python编写的程序来查找矩阵每行的最大元素。
假设我们已经输入了一个 NxN 的输入矩阵。我们将使用下面的方法来查找输入矩阵中每行的最大元素。
使用的方法
以下是实现这个任务的各种方法:
- 使用嵌套的for循环
-
使用max()函数
-
使用map()和lambda函数
方法1:使用嵌套的for循环
步骤
以下是执行所需任务的算法/步骤:
- 创建一个函数 maximumRowElement() ,通过接受输入矩阵作为参数来打印每行的最大元素。
-
通过使用len()函数计算矩阵的长度来获取矩阵的行数。
-
通过计算矩阵的任意一行的长度来获取矩阵的列数。
-
使用for循环遍历矩阵的行。
-
取一个变量来存储最大行元素并将其初始化为0。
-
使用另一个嵌套的for循环遍历矩阵的所有列。
-
检查当前元素值是否大于上面的最大变量。
-
如果上述条件成立,则将此元素设置为最大值。
-
打印最大值变量。
-
创建一个变量来存储输入矩阵,并使用嵌套的for循环打印给定的矩阵。
-
通过将输入矩阵作为参数传递给上述定义的maximumRowElement()函数来调用它,以打印矩阵中每行的最大元素。
示例
以下程序使用嵌套for循环返回输入矩阵每行的最大元素:
# creatind a function to get the maximum element in a
# row of a matrix by accepting the input matrix as an argument
def maximumRowElement(inputMatrix):
# getting the number of rows of the given matrix
rows = len(inputMatrix)
# getting the number of columns of the given matrix
cols = len(inputMatrix[0])
# traversing through the rows of the matrix
for p in range(rows):
# initializing maximum with 0 for finding a maximum element of each row
maximum = 0
# traversing through all the columns of a matrix
for q in range(cols):
# checking whether the current element value is greater than the max value
if inputMatrix[p][q] > maximum:
# assigning that element to the maximum now it becomes the maximum element
maximum = inputMatrix[p][q]
# printing the maximum element of each row of a matrix
print(maximum)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
[6, 10, 8, 11],
[7, 2, 4, 3],
[1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
# traversing through all the columns of the current row
for n in range(len(inputMatrix[0])):
# printing the corresponding element
print(inputMatrix[m][n], end=' ')
print()
print("maximum element in each row of an input matrix is:")
# calling the maximumRowElement() function by passing
# input matrix to it
maximumRowElement(inputMatrix)
输出
执行以上程序后,将生成以下输出结果:
The Given Matrix is:
5 1 3 4
6 10 8 11
7 2 4 3
1 2 3 4
maximum element in each row of an input matrix is:
5
11
7
4
方法2:使用max()函数
max()函数(返回可迭代对象中最大的元素/最大的数字)
示例
下面的程序使用max()函数返回输入矩阵的每行的最大元素-
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
[6, 10, 8, 11],
[7, 2, 4, 3],
[1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
for n in range(len(inputMatrix[0])):
# printing the corresponding element
print(inputMatrix[m][n], end=' ')
print()
print("maximum element in each row of an input matrix is:")
# traversing through each row of elements of the matrix
for k in inputMatrix:
# printing the maximum element in a row using the max() function
print(max(k))
输出
The Given Matrix is:
5 1 3 4
6 10 8 11
7 2 4 3
1 2 3 4
maximum element in each row of an input matrix is:
5
11
7
4
方法3:使用map()函数和Lambda函数
使用内置的map()函数是另一种可以用来获取矩阵每行最大元素的方法。map()方法对可迭代对象(如列表或矩阵)的每个元素应用指定的函数,以确定的方式进行处理。在这种情况下,可以使用map()方法将max()函数应用于矩阵的每一行。
示例
以下程序使用map()函数和lambda函数返回输入矩阵每一行的最大元素:
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
[6, 10, 8, 11],
[7, 2, 4, 3],
[1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
# traversing through all the columns of the current row
for n in range(len(inputMatrix[0])):
# printing the corresponding element
print(inputMatrix[m][n], end=' ')
print()
print("maximum element in each row of an input matrix is:")
# Applying max() function to each list element(row) to get maximum value
result = list(map(lambda row: max(row), inputMatrix))
# Traversing in the result list
for i in result:
# Printing the maximum row element
print(i)
输出
The Given Matrix is:
5 1 3 4
6 10 8 11
7 2 4 3
1 2 3 4
maximum element in each row of an input matrix is:
5
11
7
4
结论
在本文中,我们学习了三种不同的方法来打印给定矩阵中每行的最大值。我们还学习了如何使用map()函数对每个可迭代对象应用特定条件。