Python 3:如何对嵌套列表的子列表重新排序?
Python 3: how to re-order sublists of nested list?
我有以下嵌套列表,以及列表中子列表的数量:
l1 = [[['a','c','d'],['e','f','g'],[['a','b'],'d','1']], [['2','3','4'],['3','4','4'],[['1','2'],'3','4']], [['q1','3e','2e'],['r4','tt','t5'],[['t4','g4'],'r4','45g']]]
nb_sub = 3
我想按 'index' 对子列表重新排序,所以每个子列表的第一个子列表,然后每个子列表的第二个子列表,等等。我想要的输出是:
output = [[['a','c','d'],['2','3','4'],['q1','3e','2e']], [['e','f','g'],['3','4','4'],['r4','tt','t5']], [[['a','b'],'d','1'],[['1','2'],'3','4'],[['t4','g4'],'r4','45g']]]
zip
似乎是这项工作的完美工具:
output = [x for x in zip(*l1)]
我有以下嵌套列表,以及列表中子列表的数量:
l1 = [[['a','c','d'],['e','f','g'],[['a','b'],'d','1']], [['2','3','4'],['3','4','4'],[['1','2'],'3','4']], [['q1','3e','2e'],['r4','tt','t5'],[['t4','g4'],'r4','45g']]]
nb_sub = 3
我想按 'index' 对子列表重新排序,所以每个子列表的第一个子列表,然后每个子列表的第二个子列表,等等。我想要的输出是:
output = [[['a','c','d'],['2','3','4'],['q1','3e','2e']], [['e','f','g'],['3','4','4'],['r4','tt','t5']], [[['a','b'],'d','1'],[['1','2'],'3','4'],[['t4','g4'],'r4','45g']]]
zip
似乎是这项工作的完美工具:
output = [x for x in zip(*l1)]