【问题标题】:sorting a 2d list based on number of elements in each list (python) [duplicate]根据每个列表中的元素数对二维列表进行排序(python)[重复]
【发布时间】:2020-07-22 07:10:47
【问题描述】:

我在 python 中有这个列表:

[[153, 154], [2009, 2010, 2011], [3858, 3859, 3860], [5689, 5690]]

有没有办法根据每个列表中的元素数量对列表进行排序,结果如下:

[[2009, 2010, 2011],[3858, 3859, 3860],[153, 154], [5689, 5690]]

【问题讨论】:

    标签: python python-3.x list sorting


    【解决方案1】:

    在 python2 中,您可以执行以下操作:

    my_list = [[153, 154], [2009, 2010, 2011], [3858, 3859, 3860], [5689, 5690]]
    sorted(k, key=lambda x: len(x), reverse=True)
    

    【讨论】:

      【解决方案2】:

      试试这个:

      sorted(items,key=len, reverse=True)
      

      items 是您的列表

      【讨论】:

        猜你喜欢
        • 2021-10-28
        • 2017-07-27
        • 1970-01-01
        • 2012-03-29
        • 2012-03-11
        • 2016-05-14
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多