简体   繁体   English

numpy 数组切片索引

[英]numpy array slicing index

import numpy as np
a=np.array([ [1,2,3],[4,5,6],[7,8,9]])
  • How can I get zeroth index column?如何获得第零个索引列? Expecting output [[1],[2],[3]] a[...,0] gives 1D array.期望输出[[1],[2],[3]] a[...,0]给出一维数组。 Maybe next question answers this question.也许下一个问题可以回答这个问题。

  • How to get last 2 columns of a ?如何获得最后的2列a a[...,1:2] gives second column only, a[...,2:3] gives last 2 columns, but a[...,3] is invalid dimension. a[...,1:2]仅给出第二列, a[...,2:3]给出最后两列,但a[...,3]是无效维度。 So, how does it work?那么它是怎样工作的?

By the way, operator ... and : have same meaning?顺便说一下,运算符...:具有相同的含义吗? a[...,0] and a[:,0] give same output. a[...,0]a[:,0]给出相同的输出。 Can someone comment here?有人可以在这里发表评论吗?

numpy indexing is built on python list conventions, but extended to multi-dimensions and multi-element indexing. numpy索引建立在python列表约定之上,但扩展到多维和多元素索引。 It is powerful, but complex, but sooner or later you should read a full indexing documentation, one that distinguishes between 'basic' and 'advanced' indexing.它功能强大,但很复杂,但您迟早应该阅读完整的indexing文档,该文档区分“基本”和“高级”索引。

Like range and arange , slice index has a 'open' stop valuerangearange ,切片索引具有“开放”停止值

In [111]: a = np.arange(1,10).reshape(3,3)                                                       
In [112]: a                                                                                      
Out[112]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Indexing with a scalar reduces the dimension, regardless of where:无论在哪里,使用标量进行索引都会减少维度:

In [113]: a[1,:]                                                                                 
Out[113]: array([4, 5, 6])
In [114]: a[:,1]                                                                                 
Out[114]: array([2, 5, 8])

That also means a[1,1] returns 5 , not np.array([[5]]) .这也意味着a[1,1]返回5 ,而不是np.array([[5]])

Indexing with a slice preserves the dimension:使用切片进行索引会保留维度:

In [115]: a[1:2,:]                                                                               
Out[115]: array([[4, 5, 6]])

so does indexing with a list or array (though this makes a copy , not a view ):使用列表或数组进行索引也是如此(尽管这会生成copy ,而不是view ):

In [116]: a[[1],:]                                                                               
Out[116]: array([[4, 5, 6]])

... is a generalized : - use as many as needed. ...是一个概括: - 根据需要使用尽可能多的。

In [117]: a[...,[1]]                                                                             
Out[117]: 
array([[2],
       [5],
       [8]])

You can adjust dimensions with newaxis or reshape:您可以使用newaxis或 reshape 调整尺寸:

In [118]: a[:,1,np.newaxis]                                                                      
Out[118]: 
array([[2],
       [5],
       [8]])

Note that trailing : are automatic.请注意,尾随:是自动的。 a[1] is the same as a[1,:] . a[1]a[1,:] But leading ones must be explicit.但领导者必须明确。

List indexing also removes a 'dimension/nesting layer'列表索引还会删除“维度/嵌套层”

In [119]: alist = [[1,2,3],[4,5,6]]                                                              
In [120]: alist[0]                                                                               
Out[120]: [1, 2, 3]
In [121]: alist[0][0]                                                                            
Out[121]: 1
In [122]: [l[0] for l in alist]     # a column equivalent                                                                  
Out[122]: [1, 4]
import numpy as np
a=np.array([ [1,2,3],[4,5,6],[7,8,9]])

a[:,0] # first colomn
>>> array([1, 4, 7]) 
a[0,:] # first row
>>> array([1, 2, 3])
a[:,0:2] # first two columns
>>> array([[1, 2],
       [4, 5],
       [7, 8]])
a[0:2,:] # first two rows
>>> array([[1, 2, 3],
       [4, 5, 6]])

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

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