Convert a list with multiple sublists in an easy way

I have the following list of inputs

# 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]]
]

      

which I need to convert to

# 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]]
]

      

I can do it with

# 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])

      

but I feel like this is unnecessarily confusing.

Is there some other more elegant and / or easier way to achieve this?

+3


source to share


1 answer


Use two zip

s:

[[y for y in zip(*x)] for x in zip(*aa)]

#[[(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)]]

      




You can also use the method numpy.transpose

:

np.array(aa).transpose(1,2,0)

#array([[[  1.672     ,   1.071     ,   1.278     ,   1.071     ],
#        [ 15.40410685,  15.37432077,  15.37737475,  15.37901292]],

#       [[  1.672     ,   1.071     ,   1.278     ,   1.071     ],
#        [ 15.09023237,  14.70236476,  15.00936275,  14.70348963]],

#       [[  1.672     ,   1.071     ,   1.278     ,   1.071     ],
#        [ 14.32647735,  14.28556236,  14.32818375,  14.29081452]],

#       [[  1.672     ,   1.071     ,   1.278     ,   1.071     ],
#        [ 15.00045952,  14.96812272,  14.98111244,  14.97755947]],

#       [[  1.672     ,   1.071     ,   1.278     ,   1.071     ],
#        [ 13.15153453,  13.42926161,  13.55374351,  13.12812854]]])

      

+5


source







All Articles