【问题标题】:Find the largest number in a specified nested list containing numbers在包含数字的指定嵌套列表中查找最大数字
【发布时间】:2021-02-06 11:09:01
【问题描述】:

例如:

data = [[3, 0, 1, 1, 1, 0, 2, 1, 2, 3], 
        [0, 5, 3, 2, 2, 1, 1, 1, 3, 0], 
        [1, 3, 5, 3, 2, 1, 1, 1, 2, 1], 
        [1, 2, 3, 4, 1, 1, 2, 1, 1, 1], 
        [1, 2, 2, 1, 4, 0, 2, 2, 2, 1], 
        [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], 
        [2, 1, 1, 2, 2, 0, 4, 3, 2, 2], 
        [1, 1, 1, 1, 2, 0, 3, 3, 1, 1], 
        [2, 3, 2, 1, 2, 0, 2, 1, 5, 2], 
        [3, 0, 1, 1, 1, 0, 2, 1, 2, 4]]

我想打印嵌套列表[2, 3, 2, 1, 2, 0, 2, 1, 5, 2] 中最大的数字,即index[8][8] 中包含的5。

我还想打印它所在的嵌套列表的哪个索引。

【问题讨论】:

    标签: python list nested


    【解决方案1】:

    上面的答案很好。但是,如果您希望使用 NumPy 和 python 的优点,您可以使用以下 sn-p。如果有多个 max 元素,sn-p 也会泛化。

    import numpy as np
    
    data = [[3, 0, 1, 1, 1, 0, 2, 1, 2, 3], 
            [0, 5, 3, 2, 2, 1, 1, 1, 3, 0], 
            [1, 3, 5, 3, 2, 1, 1, 1, 2, 1], 
            [1, 2, 3, 4, 1, 1, 2, 1, 1, 1], 
            [1, 2, 2, 1, 4, 0, 2, 2, 2, 1], 
            [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], 
            [2, 1, 1, 2, 2, 0, 4, 3, 2, 2], 
            [1, 1, 1, 1, 2, 0, 3, 3, 1, 1], 
            [2, 3, 2, 1, 2, 0, 2, 1, 5, 2], 
            [3, 0, 1, 1, 1, 0, 2, 1, 2, 4]]
    
    # For max value across the matrix
    indices = np.where(data == data.max())
    max_indices = list(zip(indices[0], indices[1]))
    print(max_indices)
    [(1, 1), (2, 2), (8, 8)]
    
    # If you want it for each row
    max_args = data.max(axis=1)
    result = [(i,j,np.where(data[j]==i)[0][0]) for j,i in enumerate(max_args)]
    
    print(result)
    # Format (number,row,col)
    [(3, 0, 0),
     (5, 1, 1),
     (5, 2, 2),
     (4, 3, 3),
     (4, 4, 4),
     (1, 5, 1),
     (4, 6, 6),
     (3, 7, 6),
     (5, 8, 8),
     (4, 9, 9)]
    
    

    【讨论】:

      【解决方案2】:

      这应该对你有帮助:

      data = [[3, 0, 1, 1, 1, 0, 2, 1, 2, 3], [0, 5, 3, 2, 2, 1, 1, 1, 3, 0], [1, 3, 5, 3, 2, 1, 1, 1, 2, 1], [1, 2, 3, 4, 1, 1, 2, 1, 1, 1], [1, 2, 2, 1, 4, 0, 2, 2, 2, 1], [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], [2, 1, 1, 2, 2, 0, 4, 3, 2, 2], [1, 1, 1, 1, 2, 0, 3, 3, 1, 1], [2, 3, 2, 1, 2, 0, 2, 1, 5, 2], [3, 0, 1, 1, 1, 0, 2, 1, 2, 4]]
      
      for lst in data:
          maximum = max(lst)
          index = [data.index(lst),lst.index(maximum)]
          print(f"Largest Number = {maximum} , Index = [{index[0]}][{index[1]}]")
      

      输出:

      Largest Number = 3 , Index = [0][0]
      Largest Number = 5 , Index = [1][1]
      Largest Number = 5 , Index = [2][2]
      Largest Number = 4 , Index = [3][3]
      Largest Number = 4 , Index = [4][4]
      Largest Number = 1 , Index = [5][1]
      Largest Number = 4 , Index = [6][6]
      Largest Number = 3 , Index = [7][6]
      Largest Number = 5 , Index = [8][8]
      Largest Number = 4 , Index = [9][9]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-04
        • 2016-01-22
        • 1970-01-01
        • 2016-09-23
        • 2019-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多