Search code examples
pythonmatrixtransformtransfer

How I can transfer matrix in Python?


#Example:
A = [[1,2,3,4],[4,3,2,1],[3,2,1,4],[2,1,4,3]]
#How I can transform matrix A become matrix B 
#with matrix B I want to become...
B = [[1],
     [2],
     [3],
     [4],
     [4],
     [3],
     [2],
     [1],
     [3],
     [2],
     [1],
     [4],
     [2],
     [1],
     [4],
     [3]]

And when I save .txt or .csv how I can transfer data without [] in all of the data?


Solution

  • Write full

    A = [[1,2,3,4],[4,3,2,1],[3,2,1,4],[2,1,4,3]]
    B = []
    
    for Am in A:
      for x in Am:
        B.append([x])
    
    print (B)
    

    Write short

    A = [[1,2,3,4],[4,3,2,1],[3,2,1,4],[2,1,4,3]]
    B = [[x] for Am in A for x in Am]
    print (B)
    

    Link Example