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)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章