簡體   English   中英

Numpy.ndarray:按數組的特定軸進行迭代

[英]Numpy.ndarray: iteration by specific axes of an array

我有一個多維數組。 而且我必須通過其某些軸進行迭代。 如果需要所有軸,則可以使用nditer ,但是如果僅需要特定軸,則必須手動執行:

my_array = np.arange(3 * 4 * 5).reshape((3, 4, 5))
for i in range(my_array.shape[0]):
    for j in range(my_array.shape[1]):
        print(i, j)
        # Here should be some processing of the 3rd dimension items of the (i,j)

您不能建議我一個更簡單的方法嗎?

考慮傳遞到單個循環,並使用ndindexdocs ):

my_array = np.arange(3 * 4 * 5).reshape((3, 4, 5))
for ij in np.ndindex(my_array.shape[:2]):
    i,j=ij
    print(i,j)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM