indexing in narray

def test():
    a = np.array([[ 0,  1,  2],
                  [ 3,  4,  5],
                  [ 6,  7,  8]])
    print(a)
    print('\n')
    print(a[0:2,:]) # index the first two rows
    print('\n')
    b = np.array([True, False, True])
    print(a[b,:]) # index the first and third rows
    print('\n')
    print(a[[0,2],:]) # index the frist and thrid rows
    print('\n')
    print(a[(0,1,2),(0,1,2)]) # index the diagonal
    print('\n')
    print(a[[0,1,2],[0,1,2]]) # same as above, index the diagonal
    print('\n')
[[0 1 2]
 [3 4 5]
 [6 7 8]]


[[0 1 2]
 [3 4 5]]


[[0 1 2]
 [6 7 8]]


[[0 1 2]
 [6 7 8]]


[0 4 8]


[0 4 8]

more details see in
https://docs.scipy.org/doc/numpy/user/basics.indexing.html
https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

发布了755 篇原创文章 · 获赞 195 · 访问量 104万+

猜你喜欢

转载自blog.csdn.net/seamanj/article/details/104956260