假設我有一個ndlist類似于任意維度的 ND 陣列的嵌套串列 ( ndim) 和一個indexes帶有len(indexes) == ndim. 如果ndlist是 ND 陣列,我可以執行以下操作:
ndlist[indexes] = (some object)
串列的等價物是什么?請注意,這ndim是任意的,所以我不能像這樣對其進行硬編碼:
ndlist[indexes[0]][indexes[1]]... = (some object)
這是ndim == 3的示例:
ndlist = [[[10, 10], [10, 10]],[[10, 10], [10, 10]]] % 3-D (2x2x2) 串列,所有元素都等于 10
當我事先知道ndim 時,我可以像這樣編輯ndlist的 (0,0,0) 元素:
ndlist[0][0][0] = 11 % 從 10 到 11 的變化需要 3 [0] 秒
現在假設 ndims == 4(4 維串列)。編輯 ndlist 的 (0,0,0,0) 元素需要這樣的東西:
ndlist[0][0][0][0] = 11 % 更改為 11 需要 4 [0] 秒
對于任意 ndim:
ndlist[0][0][0]...[0] = 11 %change 到 11 需要 ndim [0]s 順序
如您所見,對于預先不知道 ndim 的一般情況,我無法以這種方式索引串列,因為它需要鍵入與 ndim 一樣多的 [0]。
如果我有一個這樣的陣列而不是串列:
ndarray = np.array(ndlist)
訪問 (0, 0, 0, ... ,0) 不會成為問題,因為我可以使用元組同時索引所有維度,如下所示:
% 3d 案例
索引 = (0,0,0)
ndarray[索引]
% 4d 案例
索引 = (0,0,0,0)
ndarray[索引]
% 案例
索引 = (0, 0, 0, ... ,0) % 我可以用代碼生成這個
ndarray[索引] = 11
Is there a way to index a list with a single tuple like that? Even better, can the tuple hold slices instead of indexes? For instance arrays also allow this:
ndarray[0:2, 0, 0] = np.array([0, 0])
The only solution have found to my problem is to use recursion to index one dimension at a time. Is there a better solution? Thanks!
uj5u.com熱心網友回復:
現在我明白了這個問題。如果您愿意擁有一個對您來說很容易的函式 donig。否則,您需要創建自己的串列型別。
功能
您將需要一個接受串列和n(未知)元素數量的函式。n你需要的手段*argv
該函式將獲取串列并獲取( ) 中的i第 th 個元素。nargv
def get_element(the_list, *argv):
sub_list = the_list.copy()
for i in argv:
sub_list = sub_list[i]
return sub_list
注意:該功能會復制原件list以確保它沒有被更改。
注意:您也需要處理index out of range錯誤。現在它會引發一個錯誤TypeError: 'blabla' object is not subscriptable。
您自己的串列型別
在這里,您將創建一個具有任何名稱(讓我們坐TypedList)的類,該類繼承list并覆寫該__getitem__方法。
class TypedList(list):
def __getitem__(self, keys):
sub_list = self.copy()
for i in keys:
sub_list = sub_list[i]
return sub_list
if __name__ == '__main__':
ndlist = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
lst = TypedList(ndlist)
print(lst[1, 1, 0])
在這里,您也需要處理index out of range錯誤。
評論后編輯。
你說的很對,我其實沒有回答這個問題。既然你已經接受了我的回答,我會想辦法做到這一點。
一些編輯
我的實作有一個小(實際上很大)的問題。如果您僅使用整數作為索引,它將失敗。所以代碼必須更改為:
class TypedList(list):
def __getitem__(self, keys):
sub_list = self.copy()
if isinstance(keys, int):
return sub_list[keys]
for i in keys:
sub_list = sub_list[i]
return sub_list
現在。我必須澄清
Python 永遠不會復制您在函式呼叫期間傳遞的物件。
這意味著什么?如果將傳遞的物件修改為函式,則物件本身將發生變化。注意我是如何先復制串列然后處理它們的。參見:https : //stackoverflow.com/a/575337/2681662
我們將通過撰寫一個實際修改傳遞物件的函式來利用這一點:
def set_value(the_list, keys, value):
for i in range(len(keys) - 1):
the_list = the_list[keys[i]]
the_list[keys[-1]] = value
現在唯一要做的就是設法使這個函式成為我們類的方法并從 __setitem__
class TypedList(list):
def __set(self, the_list, keys, value):
for i in range(len(keys) - 1):
the_list = the_list[keys[i]]
the_list[keys[-1]] = value
def __setitem__(self, keys, value):
if isinstance(keys, int):
super().__setitem__(keys, value) # <- This has to be done. Otherwise maximum recursion depth would occurre...
else:
self.__set(self, keys, value)
def __getitem__(self, keys):
sub_list = self.copy()
if isinstance(keys, int):
return sub_list[keys]
for i in keys:
sub_list = sub_list[i]
return sub_list
if __name__ == '__main__':
ndlist = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
lst = TypedList(ndlist)
lst[1, 1, 0] = 22
print(lst)
Here we have a private method called __set and will be called by __setitem__ to modify self which is a type of list.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/348940.html
