Combing 2D list of tuples and then sorting them in Python

前端 未结 3 478
星月不相逢
星月不相逢 2021-01-18 04:56

Update: The list are filled with strings I edited the list to show this

I have 3 different list such as

Section = [(\'1\', \'1.1\', \'1.2\'), (\'1\',         


        
3条回答
  •  -上瘾入骨i
    2021-01-18 05:44

    You can do:

    from itertools import chain
    
    tuples = zip(map(float, list(chain(*Section))), 
                 list(chain(*Page)), 
                 list(chain(*Title)))
    
    zip(*sorted(tuples, key=lambda x: x[0]))
    
    Out[232]:
    [(1.0, 1.0, 1.0, 1.1, 1.2, 1.2, 2.0, 2.2, 3.0, 3.2, 3.5),
     ('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'),
     ('General',
      'More',
      'Another',
      'Info',
      'Titles',
      'List',
      'Info',
      'Section',
      'Here',
      'Of',
      'Strings')]
    

    Here you first unnest your three list (what list(chain(*L)) does) and pack them in tuples with zip. Tip "tuples" to see how it looks.

    Then on the second line of code you can apply the sorting according to the element of the tuple you want. And you unpack the result.

提交回复
热议问题