Numpy——numpy的索引

1.一维索引

在元素列表或者数组中,我们可以用如同A[n]来索引某一个元素,同样的,在Numpy中也有相对应的表示方法

import numpy as np
A = np.arange(0,16)
print(A)   #一维索引
print(A[3])
A_reshape = np.arange(0,16).reshape((4,4))
print(A_reshape)
print(A_reshape[2])   #二维索引整行
print(A_reshape[2][2]) #二维索引具体

在这里插入图片描述

2.二维索引

import numpy as np
A = np.arange(0,16)
print(A)
A_reshape = np.arange(0,16).reshape((4,4))
print(A_reshape)
print(A_reshape[1][2])
print(A_reshape[1,2])   #此方法与上面方法一样
print(A_reshape[1,1:4])  #取某一行的某几个
print(A_reshape[2,:])  #取一整行

在这里插入图片描述

3.打印矩阵的行与列

import numpy as np
A = np.arange(0,16)
print(A)
A_reshape = np.arange(0,16).reshape((4,4))
print(A_reshape)
print('\n')
print(A_reshape.T)
print('\n')
for row in A_reshape:
    print(row)
print('\n')
for column in A_reshape.T:   #[记]求列要对矩阵转置一下
    print(column)

在这里插入图片描述

4.打印矩阵中的每一个

import numpy as np
A = np.arange(0,16)
print(A)
A_reshape = np.arange(0,16).reshape((4,4))
print(A_reshape)
print(A_reshape.flatten())
print("迭代器:",A_reshape.flat)
for item in A_reshape.flat:   #A_reshape.flat是一个迭代器
    print(item)

在这里插入图片描述

发布了122 篇原创文章 · 获赞 341 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_37763870/article/details/104809463