numpy迭代數(shù)組nditer的實現(xiàn)示例
1 逐個訪問數(shù)組元素
使用nditer可以完成逐個訪問數(shù)組中的元素。
a = np.arange(4).reshape(2, 2)
for i in a:
print(i)
for i in np.nditer(a):
print(i)
結(jié)果:
[0 1]
[2 3]
0
1
2
3
對于一維數(shù)組結(jié)果一樣,但是多維就有區(qū)別了
2 控制迭代順序
nditer對象提供了一個order參數(shù)來控制迭代順序。其參數(shù)order有三個可選參數(shù):K,F(xiàn),C
具有上述行為的默認值是order ='K’以保持現(xiàn)有訂單。 對于C順序,可以使用order ='C’覆蓋它,對于Fortran順序,可以使用order ='F’覆蓋它。
其中“K”是默認的,其結(jié)果與與原來的沒有區(qū)別,逐個讀取元素;
“C”:C order,即行序優(yōu)先;;
“F”:Fortran order,即列序優(yōu)先;
2.1二維
a = np.arange(6).reshape(2, 3)
print(a)
for i in np.nditer(a, order='C'):
print(i)
print("---------")
for i in np.nditer(a, order='F'):
print(i)
print("---------")
for i in np.nditer(a, order='K'):
print(i)
結(jié)果:
[[0 1 2]
[3 4 5]]
0
1
2
3
4
5
---------
0
3
1
4
2
5
---------
0
1
2
3
4
5
可以看出二維情況下"F"參數(shù)確實就是可以看過按列讀取元素
2.2三維
a = np.arange(18).reshape(2, 3, 3)
print(a)
for i in np.nditer(a, order='C'):
print(i)
print("---------")
for i in np.nditer(a, order='F'):
print(i)
print("---------")
for i in np.nditer(a, order='K'):
print(i)
結(jié)果:
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]]
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
---------
0
9
3
12
6
15
1
10
4
13
7
16
2
11
5
14
8
17
---------
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
注意三維數(shù)組列序優(yōu)先時的讀取順序。
3 修改數(shù)組值
并不推薦這種方法來修改數(shù)組值,可以作為參考
默認情況下,nditer將輸入數(shù)組視為只讀對象。 要修改數(shù)組元素,必須指定讀寫或只寫模式。 這是用每操作數(shù)標志控制的。
Python中的常規(guī)賦值只是更改本地或全局變量字典中的引用,而不是修改現(xiàn)有變量。 這意味著簡單地分配給x不會將值放入數(shù)組的元素中,而是將x作為數(shù)組元素引用切換為對指定值的引用。 要實際修改數(shù)組的元素,x應(yīng)該用省略號索引。
a = np.arange(6).reshape(2, 3)
for x in np.nditer(a, op_flags=['readwrite']):
x[...] = 2*x
print(a)
[[ 0 2 4]
[ 6 8 10]]4 使用外部循環(huán)
在目前為止的所有示例中,a的元素由迭代器一次提供一個,因為所有循環(huán)邏輯都是迭代器的內(nèi)部邏輯。 雖然這很簡單方便,但效率不高。 更好的方法是將一維最內(nèi)層循環(huán)移動到迭代器外部的代碼中。 這樣,NumPy的矢量化操作可以用在被訪問元素的較大塊上。
nditer將嘗試提供盡可能大的內(nèi)部循環(huán)塊。 通過強制’C’和’F’順序,我們得到不同的外部循環(huán)大小。 通過指定迭代器標志來啟用此模式。
a = np.arange(6).reshape(2, 3)
for x in np.nditer(a, flags=['external_loop'], order='C'):
print(x)
print("--------------")
for x in np.nditer(a, flags=['external_loop'], order='F'):
print(x)結(jié)果:注意行序優(yōu)先中輸出的一個數(shù)組,而不是一個個的數(shù)字元素
[0 1 2 3 4 5]
--------------
[0 3]
[1 4]
[2 5]
5 跟蹤索引或多索引
在迭代期間,你可能希望在計算中使用當(dāng)前元素的索引。 例如,你可能希望按內(nèi)存順序訪問數(shù)組的元素,然后使用C順序,F(xiàn)ortran順序或多維索引來查找不同數(shù)組中的值。
5.1multi_index
下面代碼中order參數(shù)根據(jù)自己需要選擇,也可以不寫默認按行逐個讀取元素
a = np.arange(6).reshape(2, 3)
it = np.nditer(a, flags=['multi_index'], order="F")
while not it.finished:
print("%d < %s>" %(it[0], it.multi_index))
it.iternext()
結(jié)果: it.multi_index代表元素的索引,以元組形式輸出
0 < (0, 0)>
3 < (1, 0)>
1 < (0, 1)>
4 < (1, 1)>
2 < (0, 2)>
5 < (1, 2)>
5.2 f_index
a = np.arange(6).reshape(2, 3)
it = np.nditer(a, flags=['f_index'])
while not it.finished:
print("%d < %d>" % (it[0], it.index))
it.iternext()結(jié)果:索引的編號,以列序優(yōu)先
0 < 0>
1 < 2>
2 < 4>
3 < 1>
4 < 3>
5 < 5>
6 緩沖數(shù)組元素
通過啟用緩沖模式,迭代器提供給內(nèi)部循環(huán)的塊可以變得更大,從而顯著減少Python解釋器的開銷。 在強制Fortran迭代順序的示例中,當(dāng)啟用緩沖時,內(nèi)部循環(huán)可以一次性查看所有元素。
a = np.arange(6).reshape(2, 3) for x in np.nditer(a, flags=['external_loop','buffered'], order='F'): print(x)
輸出:
[0 3 1 4 2 5]
7 廣播數(shù)組迭代
代碼:
a = np.arange(3)
b = np.arange(6).reshape(2,3)
for x, y in np.nditer([a,b]):
print("%d:%d" % (x,y))
輸出:
0:0
1:1
2:2
0:3
1:4
2:5
到此這篇關(guān)于numpy迭代數(shù)組nditer的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)numpy迭代數(shù)組nditer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用BeautifulSoup抓取和解析網(wǎng)頁數(shù)據(jù)的入門教程
文章介紹了Python中BeautifulSoup庫用于解析HTML和提取網(wǎng)頁數(shù)據(jù)的基本使用方法,包括安裝依賴、基礎(chǔ)用法、常用API以及實際操作示例,文中詳細解釋了BeautifulSoup與requests的關(guān)系,如何抓取和解析網(wǎng)頁數(shù)據(jù),并提供了常見錯誤和注意事項,需要的朋友可以參考下2026-05-05
python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖
不論是數(shù)據(jù)挖掘還是數(shù)據(jù)建模,都免不了數(shù)據(jù)可視化的問題,對于Python來說,Matplotlib是最著名的繪圖庫,它主要用于二維繪圖,這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖的相關(guān)資料,需要的朋友可以參考下2021-07-07
Python實現(xiàn)http服務(wù)器(http.server模塊傳參?接收參數(shù))實例
這篇文章主要為大家介紹了Python實現(xiàn)http服務(wù)器(http.server模塊傳參?接收參數(shù))實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

