每日一貼,明天的內容關鍵字為二維、呼叫-
明天就到了二維的了.python中認默是沒有帶二維的據數結構的.二維的據數結構可以通過一維的據數成組.碼代如下
class注意下呼叫情勢,這裡的是實現了 __getitem__ ,__setitem__,這裡是這樣呼叫的 如a = array2d(2,3) a[1,2] = 5array2d:
def__init__
(self,numrows,numcols):
self._therows =array(numrows)
for i in
range(numrows):
self._therows[i] =array(numcols)
defnumrows(self):
return
len(self._therows)
defnumcols(self):
return
len(self._therows[0])
defclear(self,value):
for r in
range(self.numrows()):
self._therows[r].clear(value)
def__getitem__
(self,ndxtuple):
assert len(ndxtuple) == 2,"
invalid number of array subscripts
"row =ndxtuple[0]
col = ndxtuple[1]
assert row >= 0 and row
and col >= 0 and col
"array subscript out of range
"the1darray =self._therows[row]
return
the1darray[col]
def__setitem__
(self,ndxtuple,value):
assert len(ndxtuple) == 2,"
invalid number of array subscripts
"row =ndxtuple[0]
col = ndxtuple[1]
assert row >= 0 and row
and col >= 0 and col
"array subscript out of range
"the1darray =self._therows[row]
the1darray[col] = value
當然也可以像c/c++那樣,改寫下__getitem__
def這時呼叫就是這模樣 val = a[1][2] 和a[1,2] = val,連__setitem__都不要寫了.相當於2次函式呼叫.__getitem__
(self,row):
return self._thwrows[row]
說到二維組數天然想到矩陣(matrix).
當然實際用使時是直接用使numpy庫拉.
實現碼代如下,其實只是在array2d上加了幾個簡略的作操而已.
from array import測試也很簡略.array2d
class
matrix:
def__init__
(self,numrows,numcols):
self._thegrid =array2d(numrows,numcols)
self._thegrid.clear(0)
defnumrows(self):
return
self._thegrid.numrows()
defnumcols(self):
return
self._thegrid.numcols()
def__getitem__
(self,ndxtuple):
return self._thegrid[ndxtuple[0],ndxtuple[1]]
def__setitem__
(self,ndxtuple,scalar):
self._thegrid[ndxtuple[0],ndxtuple[1]] =scalar
defscaleby(self,scalar):
for r in
range(self.numrows()):
for c in
range(self.numcols()):
self[r,c] *=scalar
deftranpose(self):
newmatrix =matrix(self.numcols(),self.numrows())
for r in
range(self.numrows()):
for c in
range(self.numcols()):
newmatrix[c,r] =self._thegrid[r,c]
return
newmatrix
def__add__
(self,rhsmatrix):
assert rhsmatrix.numrows() == self.numrows() and
\ rhsmatrix.numcols() ==self.numcols(),\
"matrix sizes not compatible for the add operation
"newmatrix =matrix(self.numrows(),self.numcols())
for r in
range(self.numrows()):
for c in
range(self.numcols()):
newmatrix[r,c] = self[r,c] +rhsmatrix[r,c]
return
newmatrix
def__sub__
(self,rhsmatrix):
assert rhsmatrix.numrows() == self.numrows() and
\ rhsmatrix.numcols() ==self.numcols(),\
"matrix sizes not compatible for the add operation
"newmatrix =matrix(self.numrows(),self.numcols())
for r in
range(self.numrows()):
for c in
range(self.numcols()):
newmatrix[r,c] = self[r,c] -rhsmatrix[r,c]
return
newmatrix
def__mul__
(self,rhsmatrix):
assert rhsmatrix.numrows() ==self.numcols() ,\
"matrix sizes not compatible for the add operation
"newr =self.numrows()
newc =rhsmatrix.numcols()
newk =self.numcols()
newmatrix =matrix(newr,newc)
for r in
range(newr):
for c in
range(newc):
temp =0
for k in
range(newk):
temp += self._thegrid[r,k]*rhsmatrix[k,c]
newmatrix[r,c] =temp
return newmatrix
嗯,好了睡覺...
資料結構 二維線段樹
顧名思義,二維線段樹就是在一棵線段樹的每乙個節點,都儲存著另一棵線段樹的根節點編號。二維線段樹通常支援以下2種功能 1 單點修改 2 二維區間查詢。為了實現這兩種功能,我們需要建一棵外層線段樹 可以動態開點也可以靜態開點 對於外層線段樹的每乙個節點,我們都儲存乙個內層線段樹的根節點編號 內層線段樹必...
資料結構與二維陣列 json xml
資料結構 二分查詢 陣列必須要從小到大排序 int search int b,int len,int key else if b mid key else if b mid key return 1 快速查詢最大值 int a int mix 0 for int i 1 i 6 i 二維陣列 1 定...
資料結構動態申請二維陣列
在實現圖操作的時候,因為用鄰接矩陣法實現,所以就用到二維陣列,而在這裡,就著重說下怎麼樣動態申請二維陣列,它的原理是 申請完畢後,就類似於在乙個一維陣列中,每乙個元素都為乙個指標,而這個指標正好指向乙個一維陣列 或者說就是這個一維陣列的首位址 當然,動態申請二維陣列和動態申請二級指標記憶體是乙個道理...