简体   繁体   English

从numpy数组中提取列

[英]Extracting Columns from numpy array

Assuming I have a numpy array 假设我有一个numpy数组

A = np.array([[1,2,3,4],[5,6,7,8]])

and I want to access it row wise I can do 我想按行访问它

for row in A:
  print(row)

which results in me having 导致我有

[1 2 3 4]
[5 6 7 8]

Is there a similar column wise method to access the array which will result in me having 是否有类似的列明智的方法来访问数组,这将导致我有

[1 5]
[2 6]
[3 7]
[4 8]

I know I can use indices but, I just want to know if there is a way to access the array column wise without indices. 我知道我可以使用索引,但是,我只想知道是否有一种方法可以在没有索引的情况下明智地访问数组列。

Thanks 谢谢

Transposing the array should get you what you want: 转置数组应该可以为您提供所需的内容:

for item in A.T:
    print(item)

The T proprety is short for the transpose() method and returns a view on the array. T属性是transpose()方法的缩写,并返回数组上的视图。

You could select the i th column of A, like so: 您可以选择A的第i列,如下所示:

for i in range(A.shape[1]):
    print(A[:, i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM