【问题标题】:Sort on value (top 5) in multidimensional array对多维数组中的值(前 5 个)进行排序
【发布时间】:2020-02-06 09:46:26
【问题描述】:

我有以下清单:

[{'id': 610603160301469715, 'worth': 0.17},
 {'id': 242730576195354624, 'worth': 0.0},
 {'id': 659311972990451712, 'worth': 0.0},
 {'id': 365975655608745985, 'worth': 0.0},
 {'id': 472141928578940958, 'worth': 0.0},
 {'id': 651499863736844298, 'worth': 0.02},
 {'id': 270904126974590976, 'worth': 0.0},
 {'id': 280497242714931202, 'worth': 0.0},
 {'id': 432610292342587392, 'worth': 0.0},
 {'id': 408785106942164992, 'worth': 0.0},
 {'id': 400900758444310528, 'worth': 0.04},
 {'id': 602213380274651400, 'worth': 0.06},
 {'id': 622692428213780481, 'worth': 0.0}]

它包含带有idworth 的项目。有没有办法按worth 排序?我真的不知道我需要搜索什么关键字。所以这就是我现在在这里问的原因。我确实尝试过搜索,但实际上找不到。

【问题讨论】:

    标签: python python-3.x list sorting dictionary


    【解决方案1】:

    您也可以使用itemgetter

    from operator import itemgetter
    sorted(l,key=itemgetter('worth'))
    

    [{'id': 242730576195354624, 'worth': 0.0},
     {'id': 659311972990451712, 'worth': 0.0},
     {'id': 365975655608745985, 'worth': 0.0},
     {'id': 472141928578940958, 'worth': 0.0},
     {'id': 270904126974590976, 'worth': 0.0},
     {'id': 280497242714931202, 'worth': 0.0},
     {'id': 432610292342587392, 'worth': 0.0},
     {'id': 408785106942164992, 'worth': 0.0},
     {'id': 622692428213780481, 'worth': 0.0},
     {'id': 651499863736844298, 'worth': 0.02},
     {'id': 400900758444310528, 'worth': 0.04},
     {'id': 602213380274651400, 'worth': 0.06},
     {'id': 610603160301469715, 'worth': 0.17}]
    

    要获得 5 个最大值,试试这个。

    top_five=sorted(l,key=itemgetter('worth'))[-5:][::-1]
    
    [{'id': 610603160301469715, 'worth': 0.17},
     {'id': 602213380274651400, 'worth': 0.06},
     {'id': 400900758444310528, 'worth': 0.04},
     {'id': 651499863736844298, 'worth': 0.02},
     {'id': 622692428213780481, 'worth': 0.0}]
    

    如果您想就地排序,请使用.sort() 方法和key=itemgetter('worth')

    l.sort(key=itemgetter('worth')) # .sort() doen't return None. And it modifies the same list.
    

    如果您要排序仅获得前 5 个最大的值。使用heapq 不需要排序。

    from heapq import nlargest
    top_five=nlargest(5,l,key=itemgetter('worth'))
    

    [{'id': 610603160301469715, 'worth': 0.17},
     {'id': 602213380274651400, 'worth': 0.06},
     {'id': 400900758444310528, 'worth': 0.04},
     {'id': 651499863736844298, 'worth': 0.02},
     {'id': 242730576195354624, 'worth': 0.0}]
    

    【讨论】:

    • @ChristianSloper 并且itemgetterlambda 快。
    【解决方案2】:

    您可以使用sorted()lambda expression 作为key

    >>> sorted(l, key=lambda x: x['worth'])
    [{'id': 242730576195354624, 'worth': 0.0},
     {'id': 659311972990451712, 'worth': 0.0},
     {'id': 365975655608745985, 'worth': 0.0},
     {'id': 472141928578940958, 'worth': 0.0},
     {'id': 270904126974590976, 'worth': 0.0},
     {'id': 280497242714931202, 'worth': 0.0},
     {'id': 432610292342587392, 'worth': 0.0},
     {'id': 408785106942164992, 'worth': 0.0},
     {'id': 622692428213780481, 'worth': 0.0},
     {'id': 651499863736844298, 'worth': 0.02},
     {'id': 400900758444310528, 'worth': 0.04},
     {'id': 602213380274651400, 'worth': 0.06},
     {'id': 610603160301469715, 'worth': 0.17}]
    

    要获得前 5 个元素:

    >>> sorted(l, key=lambda x: x['worth'], reverse=True)[:5]
    [{'id': 610603160301469715, 'worth': 0.17},
     {'id': 602213380274651400, 'worth': 0.06},
     {'id': 400900758444310528, 'worth': 0.04},
     {'id': 651499863736844298, 'worth': 0.02},
     {'id': 242730576195354624, 'worth': 0.0}]
    

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2010-10-13
      相关资源
      最近更新 更多