2017-05-05 57 views
3

我有以下的輸入數據列表以簡單的方式轉換列表幾個子列表

# 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

我可以用'numpy'陣列。目標是更容易繪製數據。 – Gabriel

+0

@Gabriel你需要列表中的所有輸出嗎? ,或者他們可以是元組? – TemporalWolf

+0

我要畫出結果,所以它們可以是元組。 – Gabriel

回答

5

使用兩個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)]] 

您還可以使用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]]]) 
+3

或'[zip(* aa)]中的[[list(zip(* x))]' –

+1

爲了獲得OP的確切輸出,可以這樣做:'[[list(y)for zip in zip (* x)]爲zip(* aa)]中的x –

相關問題