python - How do I assign a value to a multidimensional array element in a function with index and value as parameters -


i trying simple (i think). however, don't understand going on.

  • i have class creates multidimensional array
  • i want methods , set elements of array passing array index , value methods of class.
  • here code.

    from numpy import *  class space():     def __init__(self, shape, mode):         self.space = ndarray(shape, dtype=list )         self.mode = mode      def get(self, elem) :         return(self.space[elem])      def set(self, elem, val):         self.space[elem] = val         shape = [2,2,2] s = space(shape, 'wrap') s.set([1,1], [2,2]) print s.get([1,1]) 

this should straight forward process. not understanding rather basic here. explanation of going on , appreciated. thanks.

when accessing item of multi-dimensional array syntax a[i, j, k], argument inside square bracket operator tuple, not list. equivalent writing a[(i, j, k)], inside square brackets allowed omit parentheses. when calling regular function, mandatory though.

s.set((1, 1), [2, 2]) print s.get((1, 1)) 

i'd suggest overriding __getitem__() , __setitem__() instead, can use regular square bracket operator custom class.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -