Home  >  Article  >  Backend Development  >  How to get the maximum value of a single row and each column of a two-dimensional matrix in Python

How to get the maximum value of a single row and each column of a two-dimensional matrix in Python

php中世界最好的语言
php中世界最好的语言Original
2018-04-09 09:51:057190browse

This time I will bring you PythonHow to get the maximum value of each column of a single row of a two-dimensional matrix, how to get the maximum value of each column of a two-dimensional matrix in PythonWhat are the precautions, below This is a practical case, let’s take a look at it.

Because there is a small link in the project that requires this function, so I wrote a simple little function. The following is the specific implementation:

#!usr/bin/env python
#encoding:utf-8
'''
Author:沂水寒城
'''
def get_max_value(martix):
  '''
  得到矩阵中每一列最大的值
  '''
  res_list=[]
  for j in range(len(martix[0])):
    one_list=[]
    for i in range(len(martix)):
      one_list.append(int(martix[i][j]))
    res_list.append(str(max(one_list)))
  return res_list
if name == 'main':
  martix=[['1','2','3'],['3','5','0'],['5','6','2']]
  print get_max_value(martix)

The results are as follows:

['5', '6', '3']

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to convert a python string into a two-dimensional array

The object of diff is virtual dom

The above is the detailed content of How to get the maximum value of a single row and each column of a two-dimensional matrix in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn