V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Jun 10, 2016 · 1940 views
    This topic created in 3611 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   ·   803 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 20:25 · PVG 04:25 · LAX 13:25 · JFK 16:25
    ♥ Do have faith in what you're doing.