在Python中将矩阵中的列转换为列表?

2024-10-01 09:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将矩阵中的列转换为Python中的列表? 例如。 转换

test = [['Student','Ex1','Ex2','Ex3'],['Thorny','100','90','80'],['Mac','100','90','80'],['Farva','100','90','80']]

Student = ['Thorny','Mac','Farva']

请告知。你知道吗


Tags: test列表mac矩阵studentex1ex2ex3
3条回答

最简单的方法是使用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')

试试这个,我能想出的最简洁的方法: 学生=[y[0]表示测试中的y]

当然,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')]

相关问题 更多 >