如何从多维数组中提取列?
有谁知道如何在Python中从多维数组中提取列?
Does anybody know how to extract a column from a multi-dimensional array in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有谁知道如何在Python中从多维数组中提取列?
Does anybody know how to extract a column from a multi-dimensional array in Python?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(20)
如果你在Python(不是numpy)中有一个二维数组,你可以像这样提取所有列,
执行此代码将产生,
If you have a two-dimensional array in Python (not numpy), you can extract all the columns like so,
Executing this code will yield,
如果你想要第二列,你可以使用
if you want the second column you can use
一探究竟!
它与上面的东西是一样的,只是不知何故它更整洁
zip 可以完成工作,但需要单个数组作为参数, *a 语法将多维数组解压缩为单个数组参数
check it out!
it is the same thing as above except somehow it is neater
the zip does the work but requires single arrays as arguments, the *a syntax unpacks the multidimensional array into single array arguments
Python 中的 map 函数是另一种方法。
map function in Python is another way to go.
如果您喜欢map-reduce 风格的python,而不是列表推导式,那么 itemgetter 运算符也可以提供帮助,以获得一点变化!
The itemgetter operator can help too, if you like map-reduce style python, rather than list comprehensions, for a little variety!
您也可以使用它:
注意:这不适用于内置数组且未对齐(例如 np.array([[1,2,3],[4,5,6,7]]) )
You can use this as well:
Note: This is not working for built-in array and not aligned (e.g. np.array([[1,2,3],[4,5,6,7]]) )
假设我们有
n X m
矩阵(n
行和m
列),例如 5 行和 4 列要在 python 中提取列,我们可以使用这样的列表理解
您可以将 4 替换为矩阵具有的任意数量的列。
结果是
[ [1,5,9,13,17],[2,10,14,18],[3,7,11,15,19],[4,8,12,16, 20]]
let's say we have
n X m
matrix(n
rows andm
columns) say 5 rows and 4 columnsTo extract the columns in python, we can use list comprehension like this
You can replace 4 by whatever number of columns your matrix has.
The result is
[ [1,5,9,13,17],[2,10,14,18],[3,7,11,15,19],[4,8,12,16,20] ]
我认为您想从数组中提取一列,例如下面的数组
现在,如果您想获取格式中的第三列
那么您需要首先将数组设为矩阵
现在您可以像您一样进行元素明智的计算在excel中做。
I think you want to extract a column from an array such as an array below
Now if you want to get the third column in the format
Then you need to first make the array a matrix
And now you can do element wise calculations much like you would do in excel.
使用矩阵的另一种方法
One more way using matrices
只需使用 transpose(),即可像获取行一样轻松获取列
Just use transpose(), then you can get the columns as easy as you get rows
如果您想获取不止一列,只需使用切片:
If you want to grab more than just one column just use slice:
好吧,有点晚了……
如果性能很重要并且您的数据是矩形的,您也可以将其存储在一维中并通过常规切片访问列,例如……
巧妙的是这非常快。 但是,负索引在这里不起作用! 因此您无法通过索引 -1 访问最后一列或最后一行。
如果您需要负索引,您可以稍微调整访问器函数,例如
Well a 'bit' late ...
In case performance matters and your data is shaped rectangular, you might also store it in one dimension and access the columns by regular slicing e.g. ...
The neat thing is this is really fast. However, negative indexes don't work here! So you can't access the last column or row by index -1.
If you need negative indexing you can tune the accessor-functions a bit, e.g.
尽管使用
zip(*iterable)
来转置嵌套列表,但如果嵌套列表的长度不同,您也可以使用以下内容:结果:
第一列因此:
Despite using
zip(*iterable)
to transpose a nested list, you can also use the following if the nested lists vary in length:results in:
The first column is thus:
我更喜欢下一个提示:
具有名为
matrix_a
的矩阵并使用column_number
,例如:I prefer the next hint:
having the matrix named
matrix_a
and usecolumn_number
, for example:矩阵中的所有列都放入新列表中:
All columns from a matrix into a new list:
难道您正在使用 NumPy 数组? Python 有 array 模块,但不支持多维数组。 普通的 Python 列表也是一维的。
但是,如果您有一个像这样的简单二维列表:
那么您可以像这样提取一列:
提取第二列(索引 1):
或者,简单地:
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:
then you can extract a column like this:
Extracting the second column (index 1):
Or alternatively, simply:
如果您有一个像这样的数组
,那么您可以像这样提取第一列:
所以结果如下所示:
If you have an array like
Then you extract the first column like that:
So the result looks like this:
另请参阅:“numpy.arange”和“reshape”来分配内存
示例:(通过矩阵形状 (3x4) 分配数组)
See also: "numpy.arange" and "reshape" to allocate memory
Example: (Allocating a array with shaping of matrix (3x4))