multidimensional array - How can I chage the index count in a matrix? (Python) -


i have multidimensional array looks this:

matrix = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]] 

if wanted find value in first spot inside matrix have this:

matrix[0][0] 

is there way in can make instead of having input coordinates find position can ask specific position?

so if wanted first position input

matrix[1] 

instead of

matrix[0][0] 

if wanted second position input

matrix[2] 

instead of

matrix[1][0] 

and on.

thanks help.

one of simplest (and cleanest) solution wrap matrix inside class, , define bracket operator class. however, aware usually, positions go 0 n-1, not 1 n.

the code compatible both python 2 , python 3

class matrix:      def __init__(self, matrix):         self.matrix = matrix         self.n = len(matrix)      def __getitem__(self, index):         index -= 1         return self.matrix[int(index%self.n)][int(index/self.n)]  = [[1,5,9,13],      [2,6,10,14],      [3,7,11,15],      [4,8,12,16]]  mtx = matrix(a) print([ mtx[i] in range(1,17) ]) # [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -