如何在Python中将矩阵中的列转换为列表? 例如。 转换
test = [['Student','Ex1','Ex2','Ex3'],['Thorny','100','90','80'],['Mac','100','90','80'],['Farva','100','90','80']]
到
Student = ['Thorny','Mac','Farva']
请告知。
答案 0 :(得分:2)
尝试一下,这是我想出的最紧凑的方法: 学生= [y [0] for test in y]
当然可以将0更改为您想要的。
答案 1 :(得分:0)
最简单的方法是使用numpy进行索引:
import numpy as np
convert_test = [['Student','Ex1','Ex2','Ex3'],['Thorny','100','90','80'],['Mac','100','90','80'],['Farva','100','90','80']]
convert_test = np.array(convert_test)
print(convert_test[:,0])
array(['Student','Thorny','Mac','Farva'],dtype ='U7')
答案 2 :(得分:0)
Student = list(zip(*test))[0][1:]
where
>>>list(zip(*test))
[('Student', 'Thorny', 'Mac', 'Farva'),
('Ex1', '100', '100', '100'),
('Ex2', '90', '90', '90'),
('Ex3', '80', '80', '80')]