我有以下的輸入數據列表以簡單的方式轉換列表幾個子列表
# 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])
但我覺得這是不必要的複雜。
是否還有其他更多優雅和/或更簡單的方法來實現?
我可以用'numpy'陣列。目標是更容易繪製數據。 – Gabriel
@Gabriel你需要列表中的所有輸出嗎? ,或者他們可以是元組? – TemporalWolf
我要畫出結果,所以它們可以是元組。 – Gabriel