📌  相关文章
📜  Python|矩阵中第 N 列的最大值/最小值

📅  最后修改于: 2022-05-13 01:54:29.962000             🧑  作者: Mango

Python|矩阵中第 N 列的最大值/最小值

有时,在使用Python Matrix 时,我们可能会遇到需要找到特定列的最小值和最大值的问题。这可以在日间编程和竞争性编程中具有可能的应用。让我们讨论一下可以执行此任务的特定方式。

方法:使用max()/min() + zip()
可以使用上述功能的组合来解决此任务。在此,我们传入 zip() 列表,以访问所有列,并通过max()/min()获取最大或最小列。

# Python3 code to demonstrate working of
# Max value in Nth Column in Matrix
# using max() + zip()
  
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2], 
             [10, 3, 4]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initialize N 
N = 2
  
# Max value in Nth Column in Matrix
# using max() + zip()
res = [max(i) for i in zip(*test_list)][N] 
  
# printing result
print("Max value of Nth column is : " + str(res))
输出 :
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Max value of Nth column is : 7