Transposing a 3D list in Python

后端 未结 2 1622
遇见更好的自我
遇见更好的自我 2021-01-18 22:21

I have to transpose a 3d list the following way:

Input:

matrix7 = [[[\'A \', \'E \', \'C#\'], [\'B \', \'E \', \'C#\'], [\'C \', \'E \', \'C#\']], 
[         


        
2条回答
  •  一整个雨季
    2021-01-18 22:45

    I think this is what you want:

    numpy.transpose(matrix7, axes=(1, 0, 2)).tolist()  # The 'axes' attribute tells transpose to swaps axes 0 and 1, leaving the last one alone.
    

    OUTPUT:

    [[['A ', 'E ', 'C#'], ['  ', 'F#', 'D '], ['  ', 'E ', 'B '], ['  ', 'E ', 'C#'], ['  ', 'F#', 'D '], ['  ', 'E ', 'B '], ['  ', '  ', '  '], ['  ', 'E ', 'C#']],
    [['B ', 'E ', 'C#'], ['  ', 'F#', 'D '], ['  ', 'E ', 'B '], ['  ', 'E ', 'C#'], ['  ', 'F#', 'D '], ['  ', 'E ', 'B '], ['  ', '  ', '  '], ['  ', 'E ', 'C#']],
    [['C ', 'E ', 'C#'], ['  ', 'F#', 'D '], ['  ', 'E ', 'B '], ['  ', 'E ', 'C#'], ['  ', 'F#', 'D '], ['  ', 'E ', 'B '], ['  ', '  ', '  '], ['  ', 'E ', 'C#']]]
    

提交回复
热议问题