以简单的方式转换包含多个子列表的列表

时间:2017-05-05 21:21:36

标签: python list

我有以下输入数据列表

# Input
aa = [
    [[1.672, 15.404106853988145], [1.672, 15.09023236677281],
     [1.672, 14.326477347851142], [1.672, 15.000459521550175],
     [1.672, 13.151534525114727]],
    [[1.071, 15.37432077372023], [1.071, 14.702364761334275],
     [1.071, 14.285562362585377], [1.071, 14.968122718390234],
     [1.071, 13.429261607006364]],
    [[1.278, 15.37737475413325], [1.278, 15.009362745118009],
     [1.278, 14.328183750447181], [1.278, 14.98111244150097],
     [1.278, 13.553743514980896]],
    [[1.071, 15.37901291568889], [1.071, 14.703489628076182],
     [1.071, 14.290814520089102], [1.071, 14.977559474289448],
     [1.071, 13.128128537587324]]
]

我需要转换成

# Output
bb = [
    [[1.672, 1.071, 1.278, 1.071],
     [15.404106853988145, 15.37432077372023, 15.37737475413325,
      15.37901291568889]],
    [[1.672, 1.071, 1.278, 1.071],
     [15.09023236677281, 14.702364761334275, 15.00936274511801,
      14.703489628076182]],
    [[1.672, 1.071, 1.278, 1.071],
     [14.326477347851142, 14.285562362585377, 14.328183750447181,
      14.290814520089102]],
    [[1.672, 1.071, 1.278, 1.071],
     [15.000459521550175, 14.968122718390234, 14.98111244150097,
     14.977559474289448]],
    [[1.672, 1.071, 1.278, 1.071],
     [13.151534525114727, 13.429261607006364, 13.553743514980896,
     13.128128537587324]]
]

我可以用

做到这一点
# Transform 'aa' into 'bb'
bb = [[[], []] for _ in aa[0]]
cc = [zip(*_) for _ in aa]
for a in cc:
    aa = zip(*a)
    for i, a in enumerate(aa):
        bb[i][0].append(a[0])
        bb[i][1].append(a[1])

但我觉得这是不必要的错综复杂的。

还有其他更优雅和/或更简单的方法来实现这一目标吗?

0 个答案:

没有答案