【问题标题】:How to print index for row and column of smallest value [duplicate]如何打印最小值的行和列的索引[重复]
【发布时间】:2021-07-30 19:40:48
【问题描述】:

我正在尝试打印数组中最小数字的索引。数组是:

import numpy as np

z = np.array(
    [
        [2.3, 5.9, 0.01],
        [7.7, 2.22, 1.02],
        [0.5, 2.7, 9.8],
        [3.76, 5.323, 5.24],
    ]
)

所以最小值是 0.01。但是当我打印这个索引时:

new = np.argmin(z)

我的输出是 2,它只是列的索引。但是,我还想要为 0 的行的索引。有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    可能有一种更简单的方法,但这很有效:

    x=np.unravel_index(np.argmin(z), z.shape, order='C')
    print(x)
    

    输出:

    (0,2) #row, column
    

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2020-05-08
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多