【问题标题】:how do I find the index of row in an array that the first column has a specific value and the second column has the max value?如何在第一列具有特定值且第二列具有最大值的数组中找到行的索引?
【发布时间】:2022-06-26 00:05:14
【问题描述】:

假设我有一个数组a,大小为(n,2),如下所示:

a=
6   185.153
6   9.50864
1   9.31425
1   16.4629
6   19.6971
1   2.02113
1   14.0193
5   2.92495
3   56.0731
3   77.6965

现在我需要找到第一列是特定值M(例如3)的行的索引,并且第二个对应列在其他行之间具有最大值,第一列等于M。例如在上面的数组中,索引将是8 我使用了以下代码,但它不起作用并且输出错误。你知道是什么问题吗?

indx_nonremoved=np.where([minimum_merge.max(axis=1) ==3 ])[1]

【问题讨论】:

    标签: python arrays python-3.x numpy


    【解决方案1】:

    您需要使用列表推导和max() 内置函数的key 参数:

    a=[[6,   185.153],
    [6,   9.50864],
    [1 ,  9.31425],
    [1  , 16.4629],
    [6   ,19.6971],
    [1   ,2.02113],
    [1   ,14.0193],
    [5   ,2.92495],
    [3   ,56.0731],
    [3   ,77.6965]]
    
    print(a.index(max([i for i in a if i[0]==3], key=lambda x : x[1])))
    

    输出:

    9
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2021-08-03
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多