我有一个由2个浮点数的4个列表组成的2个列表,例如:
L1 = [ [0,1], [3,4], [2,5], [4,1] ]
L2 = [ [4,3], [3,5], [2,2], [1,3] ]
我如何将其重塑为:
L_combined = [[L1 first index of each sublist],
[L2 first index of each sublist],
[L1 second index of each sublist],
[L2 second index of each sublist]]
要清楚,这应该导致:
[[0,3,2,4],
[4,3,2,1],
[1,4,5,1],
[3,5,2,3]]
对于合并的一般情况,我该怎么做:
L1 = [[1, 2, .. Y], [1, 2, .. Y] ...]
L2 = [[1, 2, .. Y], [1, 2, .. Y] ...]
...
LX = [[1, 2, .. Y], [1, 2, .. Y] ...]
进入
L_combined = [[L1 first index of each sublist],
[L2 first index of each sublist],
[....]
[....],
[LX Yth index of each sublist]]
谢谢!
答案 0 :(得分:2)
您可以使用:
output = [[i[0] for i in list1], [i[0] for i in list2], [i[1] for i in list1], [i[1] for i in list2]]
您可以使用:
lsts = [lst1, lst2, lst3 ...]
output = [[j[k] for j in i] for k in range(len(lsts[0][0])) for i in lsts]
这是假设所有列表都相同
输入:
lst1 = [['a11', 'a21', 'a31'],['a12', 'a22', 'a32'],['a13', 'a23', 'a33']]
lst2 = [['b11', 'b21', 'b31'],['b12', 'b22', 'b32'],['b13', 'b23', 'b33']]
lst3 = [['c11', 'c21', 'c31'],['c12', 'c22', 'c32'],['c13', 'c23', 'c33']]
输出:
[['a11', 'a12', 'a13'], ['b11', 'b12', 'b13'], ['c11', 'c12', 'c13'], ['a21', 'a22', 'a23'], ['b21', 'b22', 'b23'], ['c21', 'c22', 'c23'], ['a31', 'a32', 'a33'], ['b31', 'b32', 'b33'], ['c31', 'c32', 'c33']]