#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    This Must be a Simple Error


    I'm using unadorned Python 2.7.3, and I run the following code in an interpreter:
    Code:
    from itertools import product 
    class Operation(object):
        def __init__(self,arity,universe,table):
            self.arity=arity
            self.universe=universe
            self.table=-1
            for i in range(self.arity): 
                self.table=[self.table]*self.universe 
            for x in product(range(self.universe),repeat=self.arity):
                self._set_value(x,table(*x))
    
        def _set_value(self,x,v):
            t=self.table
            for i in range(len(x)-1):
                t=t[x[i]]
            if isinstance(t,list) and all([ isinstance(a,int) for a in t ]):
                t[x[-1]]=v
    p0=Operation(2,2,lambda x,y:x)
    p0.table
    I should get [[0,0],[1,1]]. Instead I keep getting [[1,1],[1,1]]. What am I doing wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    514
    Rep Power
    33
    This is a common beginner error. The problem is this line
    Code:
    self.table=[self.table]*self.universe
    You are creating a list that contains self.universe number of the same field, self.table, so when you change one they all change since they are the same. This code with a print statement added will show that they all have the same id's.
    Code:
    rom itertools import product 
    class Operation(object):
        def __init__(self,arity,universe,table):
            self.arity=arity
            self.universe=universe
            self.table=-1
            for i in range(self.arity): 
                self.table=[self.table]*self.universe 
            ##------- print id's
            for idx in self.table:
                print idx,
                for ctr in range(self.universe):
                    print id(idx[ctr]),
                print
    
            for x in product(range(self.universe),repeat=self.arity):
                self._set_value(x,table(*x))
    
        def _set_value(self,x,v):
            t=self.table
            for i in range(len(x)-1):
                t=t[x[i]]
            if isinstance(t,list) and all([ isinstance(a,int) for a in t ]):
                t[x[-1]]=v
    p0=Operation(2,2,lambda x,y:x)
    You want to use list comprehension or a for loop to create the initial list.
    Code:
    initial_value = -1
    universe = 3
    table=[[initial_value, initial_value] for ctr in range(universe )]
    print table
    table[0][1] = "X"
    print table
    Last edited by dwblas; August 9th, 2013 at 04:38 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0
    Actually, I want a list which contains self.universe many of the same field. All that for loop is doing is constructing an a-dimensional n-side-length cubic array of -1's, which are then individually changed to reflect the outputs of the function "table". The reason I am using a list of lists (of lists etc) in this way is that I intend to change it into a tuple of tuples (of tuples etc) after constructing it, so that it becomes meaningfully hashable. If I didn't need it to be hashable, I would instead be using a dictionary whose keys are a-length tuples and values are ints.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    514
    Rep Power
    33
    Using a for loop (untested):
    Code:
    self.table = []
    for row in range(num_rows):
        junk_list = []     ## initialize each time to get different blocks of memory for each junk_list
        for col in range(num_cols):
            junk_list.append(-1)
        self.table.append(junk_list)
    #
    # or
    for row in range(num_rows):
        self.table.append([-1 for ctr in range(num_cols)])
    Last edited by dwblas; August 9th, 2013 at 04:46 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0
    You seem to have missed the part where I said it's an a-dimensional array. This means that if we have an arity of 5, it's a 5-dimensional array. What you've said, as far as I can tell, will only ever give me a 2-dimensional array.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0
    Oh wow, I finally see what you're saying. I'm creating a list that has only copies of the same pointer, rather than copies of the same list.
    Thanks for your help.

IMN logo majestic logo threadwatch logo seochat tools logo