Matrix Transpose in Python

前端 未结 18 1712
甜味超标
甜味超标 2020-11-22 00:21

I am trying to create a matrix transpose function for python but I can\'t seem to make it work. Say I have

theArray = [[\'a\',\'b\',\'c\'],[\'d\',\'e\',\'f\         


        
18条回答
  •  走了就别回头了
    2020-11-22 01:08

    Much easier with numpy:

    >>> arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
    >>> arr
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    >>> arr.T
    array([[1, 4, 7],
           [2, 5, 8],
           [3, 6, 9]])
    >>> theArray = np.array([['a','b','c'],['d','e','f'],['g','h','i']])
    >>> theArray 
    array([['a', 'b', 'c'],
           ['d', 'e', 'f'],
           ['g', 'h', 'i']], 
          dtype='|S1')
    >>> theArray.T
    array([['a', 'd', 'g'],
           ['b', 'e', 'h'],
           ['c', 'f', 'i']], 
          dtype='|S1')
    

提交回复
热议问题