【问题标题】:Find max value from list having single element从具有单个元素的列表中查找最大值
【发布时间】:2022-01-12 22:29:49
【问题描述】:

列表包含具有数组值的单个元素。如何从列表中找到最大值? 列表是 [array([[ 4, 5, 6], [10, 11, 12]])]。查找以下代码以供参考。

import numpy
a=np.arange(1,13).reshape(2,2,3)
pt=[np.array([0,1]),np.array([1,1])]
list=[]
list.append(a[pt[0],pt[1]])

【问题讨论】:

  • 注意:list 是一个 python 内置函数。您可能不想将其用作变量名。无论如何,要在 numpy 数组中获得最大值,请使用 np.max。所以np.max(list[0])应该会给你答案。

标签: python list max


【解决方案1】:

您可以使用numpy max function 找到 numpy 数组的最大值:

m = np.max(list[0])

【讨论】:

    【解决方案2】:

    首先 import numpy as np 因为第 2 行,当您将 numpy 模块称为 np. 这是我为您找到解决方案的代码,肯定有更优雅的答案

    holder = 0
    for x in a:
        for y in x:
            for z in range(len(y)):
                if holder < y[z]:
                    holder = y[z]
    print(holder)
    

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2021-10-10
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多