Matrix Transpose in Python

前端 未结 18 1227
甜味超标
甜味超标 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 00:55

    >>> theArray = [['a','b','c'],['d','e','f'],['g','h','i']]
    >>> [list(i) for i in zip(*theArray)]
    [['a', 'd', 'g'], ['b', 'e', 'h'], ['c', 'f', 'i']]
    

    the list generator creates a new 2d array with list items instead of tuples.

提交回复
热议问题