0

I need to keep track of some process which goes like this

time0 = [  [0]  ]  
time1 = [  [ time0 ] , [1]  ]  
time2 = [  [ time1 ] , [2]  ]  
time3 = [  [ time2 ] , [3]  ]  
...
time5 = [  [ time4 ] , [5]  ]  

I do like this because:
- I need the inner objects, in a time5 object, to be dependent, so that if I modify time0, all time1 to time5 see the change;
- I create a million of those time5 objects, and my guess is that the previous process will be easy on memory;

Now, I have a hard time to access the inner element of time5. I would hard code

time5[0][0][0][0][0][0] = 0   
time5[0][0][0][0][1][0] = 1  
time5[0][0][0][1][0] = 2  
time5[0][0][1][0] = 3
time5[0][1][0] = 4
time5[1][0] = 5

but I need a function which will produce those index [0][0][1][0], for any arbitrary timeX object.
I don't see how to do that.

I'm also open to any design suggestion.
I suspect my idea is not the best.

  • 1
    you clearly want to use an Linked-List : http://stackoverflow.com/questions/280243/python-linked-list – lucasg Jun 26 '13 at 11:00

1 Answers1

2

First here's the solution you are looking for (I guess):

def access(time_object, max_lvl, lvl):
    tmp = time_object
    while(lvl < max_lvl):
        tmp = time_object[0]
    return tmp

Now to be honest, I don't see why you don't simply store every element in an array. The reason why you don't need to go with your complicated approach is that it seems that the dependency relation is sequential: timeN = [ [ timeN-1 ], [N] ]

let's say you put every time object in a list [ time1, time2, ...., timeN] you can easily see that

timeN[0][0][0][0][1][0] = list[N-4][0]
Samy Arous
  • 6,794
  • 13
  • 20