>>> def f(x,y):
 ...     return 10*x+y
 ...
 >>> b = fromfunction(f,(5,4),dtype=int)
 >>> b
 array([[ 0,  1,  2,  3],
        [10, 11, 12, 13],
        [20, 21, 22, 23],
        [30, 31, 32, 33],
        [40, 41, 42, 43]])
 >>> b[2,3]
 23
 >>> b[0:5,1]
 array([ 1, 11, 21, 31, 41])
 >>> b[ : ,1]
 array([ 1, 11, 21, 31, 41])
 >>> b[1:3, : ]
 array([[10, 11, 12, 13],
        [20, 21, 22, 23]])
 >>> b[-1]
 array([40, 41, 42, 43])
 >>> b[ ... ,1]
 array([ 1, 11, 21, 31, 41])
 >>> b[1,:]
 array([10, 11, 12, 13])
 >>> b[1,...]
 array([10, 11, 12, 13])
 >>> b[...]
 array([[ 0,  1,  2,  3],
        [10, 11, 12, 13],
        [20, 21, 22, 23],
        [30, 31, 32, 33],
        [40, 41, 42, 43]])
 >>> c = array( [ [[  0,  1,  2],
 ... [ 10, 12, 13]],
 ... [[100,101,102],
 ... [110,112,113]] ] )
 >>> c
 array([[[  0,   1,   2],
         [ 10,  12,  13]],       [[100, 101, 102],
         [110, 112, 113]]])
 >>> c.shape (2, 2, 3)
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: 'tuple' object is not callable
 >>> c.shape(2, 2, 3)
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: 'tuple' object is not callable
 >>> c.shape
 (2, 2, 3)
 >>> c
 array([[[  0,   1,   2],
         [ 10,  12,  13]],       [[100, 101, 102],
         [110, 112, 113]]])
 >>> c[1,...]
 array([[100, 101, 102],
        [110, 112, 113]])
 >>> c[1,:,:]
 array([[100, 101, 102],
        [110, 112, 113]])
 >>> c[1]
 array([[100, 101, 102],
        [110, 112, 113]])
 >>> c[...,2]
 array([[  2,  13],
        [102, 113]])
 >>> c.shape
 (2, 2, 3)
 >>> d=c[...,2]
 >>> d.shape
 (2, 2)
 >>> c[:,:,2]
 array([[  2,  13],
        [102, 113]])
 >>> b
 array([[ 0,  1,  2,  3],
        [10, 11, 12, 13],
        [20, 21, 22, 23],
        [30, 31, 32, 33],
        [40, 41, 42, 43]])
 >>> for row in b:
 ...         print row
 ...
 [0 1 2 3]
 [10 11 12 13]
 [20 21 22 23]
 [30 31 32 33]
 [40 41 42 43]
 >>> for element in b.flat:
 ...         print element,
 ...
 0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
 >>> c=b.flat
 >>> c
 <numpy.flatiter object at 0x02153BF0>
 >>> type(b)
 <type 'numpy.ndarray'>
 >>> type(c)
 <type 'numpy.flatiter'>
 >>> c=array(c)
 >>> c
 array([ 0,  1,  2,  3, 10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33, 40,
        41, 42, 43])
 >>> type(c)
 <type 'numpy.ndarray'>
 >>>

索引,切片和迭代

一维 数组可以被索引、切片和迭代,就像 列表 和其它Python序列。

>>> a = arange(10)**3
>>> a
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000    # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])
>>> a[ : :-1]                                 # reversed a
array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])
>>> for i in a:
...         print i**(1/3.),
...
nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0
>>> a = arange(10)**3
>>> a
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000    # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])
>>> a[ : :-1]                                 # reversed a
array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])
>>> for i in a:
...         print i**(1/3.),
...
nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0

多维

>>> def f(x,y):
...         return 10*x+y
...
>>> b = fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])
>>> b[2,3]
23
>>> b[0:5, 1]                       # each row in the second column of b
array([ 1, 11, 21, 31, 41])
>>> b[ : ,1]                        # equivalent to the previous example
array([ 1, 11, 21, 31, 41])
>>> b[1:3, : ]                      # each column in the second and third row of b
array([[10, 11, 12, 13],
       [20, 21, 22, 23]])
>>> def f(x,y):
...         return 10*x+y
...
>>> b = fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])
>>> b[2,3]
23
>>> b[0:5, 1]                       # each row in the second column of b
array([ 1, 11, 21, 31, 41])
>>> b[ : ,1]                        # equivalent to the previous example
array([ 1, 11, 21, 31, 41])
>>> b[1:3, : ]                      # each column in the second and third row of b
array([[10, 11, 12, 13],
       [20, 21, 22, 23]])

当少于轴数的索引被提供时,确失的索引被认为是整个切片:

>>> b[-1]                                  # the last row. Equivalent to b[-1,:]
array([40, 41, 42, 43])
>>> b[-1]                                  # the last row. Equivalent to b[-1,:]
array([40, 41, 42, 43])

b[i] 中括号中的表达式被当作 i 和一系列 : ,来代表剩下的轴。NumPy也允许你使用“点”像 b[i,...]

  • x[1,2,…] 等同于 x[1,2,:,:,:],
  • x[…,3] 等同于 x[:,:,:,:,3]
  • x[4,…,5,:] 等同 x[4,:,:,5,:].
>>> c = array( [ [[  0,  1,  2],      # a 3D array (two stacked 2D arrays) ...               [ 10, 12, 13]], ... ...              [[100,101,102], ...               [110,112,113]] ] ) >>> c.shape (2, 2, 3) >>> c[1,...]                          # same as c[1,:,:] or c[1] array([[100, 101, 102],        [110, 112, 113]]) >>> c[...,2]                          # same as c[:,:,2] array([[  2,  13],        [102, 113]])


迭代 多维数组是就第一个轴而言的: 2

>>> for row in b:
...         print row
...
[0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]

然而,如果一个人想对每个数组中元素进行运算,我们可以使用flat属性,该属性是数组元素的一个迭代器:

>>> for element in b.flat:
...         print element,
...
0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
>>> for element in b.flat:
...         print element,
...
0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43

更多[], …, newaxis, ndenumerate, indices, index exp 参考 NumPy示例