coolair
V2EX  ›  问与答

如何优雅的对一个 Python List 进行重新组合?

  •  
  •   coolair · Jun 10, 2016 · 2037 views
    This topic created in 3711 days ago, the information mentioned may be changed or developed.
    原 List 如下:
    [['a', '1', '2', '3'], ['a', '2', '3', '4'], ['b', '3', '4', '5'], ['b', '4', '5', '6']]
    想要变成如下形式:
    [
    [a, [[1, 2, 3], [2, 3, 4]]],
    [b, [[3, 4, 5], [4, 5, 6]]]
    ]
    怎样做更优雅方便呢?
    6 replies    2016-06-11 09:34:37 +08:00
    pimin
        1
    pimin  
       Jun 10, 2016 via Android
    感觉做成字典更符合你需求
    coolair
        2
    coolair  
    OP
       Jun 10, 2016
    @pimin 字典排序就乱了。
    lll9p
        3
    lll9p  
       Jun 10, 2016
    from collections import OrderedDict
    imn1
        4
    imn1  
       Jun 10, 2016
    pandas 做这个容易
    thinker3
        5
    thinker3  
       Jun 10, 2016
    google python groupby list
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1024 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 23:29 · PVG 07:29 · LAX 16:29 · JFK 19:29
    ♥ Do have faith in what you're doing.