我想知道如何将python中的用户输入更改为列表,或者更好的是矩阵,就像使用int(输入)将其转换为整数一样。
答案 0 :(得分:4)
>>> L = [[1,2,3], [4,5,6], [7,8,9]]
>>> [[x[i] for x in L] for i in range(len(L[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
或
>>> zip(*L)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
或
>>> import numpy as np
>>> L = np.arange(1, 10).reshape((3, 3))
>>> L
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> L.transpose()
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
答案 1 :(得分:4)
array([[1,2,3], [4,5,6], [7,8,9]]).T
会执行您想要的操作。
答案 2 :(得分:0)
def transpose(the_array):
return [[the_array[j][i] for j in range(0, len(the_array[i]))] for i in range(0, len(the_array))]