立即学习:https://edu.csdn.net/course/play/24797/282184?utm_source=blogtoedu
定义计算矩阵转置的函数
import numpy testval = [[1, 2, 3, 4, 5, ], [6, 7, 8, 9, 10], ['a', 'b', 'c', 'd', 'e']] # for示例---------------------------------- def testfor(val): for ele in val: for i in ele: print(i, end=" ") print(" ") testfor(testval) # for 转置 def zhuanzhi(val): newtv = [[] for i in val[0]] for ele in val: for i in range(len(ele)): newtv[i].append(ele[i]) return newtv print('-' * 20) testfor(zhuanzhi(testval)) # zip 转置--------------------------------- def zipzhuanzhi(val): return list(zip(*val)) print('-' * 20) testfor(zipzhuanzhi(testval)) # numpy 转置------------------------------- def numpyzhuanzhi(val): return numpy.transpose(val).tolist() print('-' * 20) testfor(numpyzhuanzhi(testval))