你如何提取一个多维阵列的列? [英] How do you extract a column from a multi-dimensional array?
本文介绍了你如何提取一个多维阵列的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有人知道如何从Python中的多维数组中提取一列?
Does anybody know how to extract a column from a multi-dimensional array in Python?
推荐答案
难道您使用的是<一个href=\"http://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays\">NumPy阵列? Python有 模块,但不支持多维数组。普通的Python列表是单维的了。
Could it be that you're using a NumPy array? Python has the array module, but that does not support multi-dimensional arrays. Normal Python lists are single-dimensional too.
不过,如果你有一个简单的二维表是这样的:
However, if you have a simple two-dimensional list like this:
A = [[1,2,3,4],
[5,6,7,8]]
然后就可以提取一列如下:
then you can extract a column like this:
def column(matrix, i):
return [row[i] for row in matrix]
提取的第二列(索引1):
Extracting the second column (index 1):
>>> column(A, 1)
[2, 6]
或者,简单地说:
Or alternatively, simply:
>>> [row[1] for row in A]
[2, 6]
这篇关于你如何提取一个多维阵列的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文