Search code examples
pythonpython-3.xlistoverlapoverlapping

Overlap of nested lists creates unwanted gap


I have a nest containing three lists which are being filled by a for-loop and the fill is controlled by if-conditions.

After the first iteration it could look like the following example:

a = [[1,2,0,0,0,0],[0,0,4,5,0,0],[0,0,0,0,6,7]] 

which, by condition, are not overlapping. After the second iteration new values are being appended to the corresponding nested lists. In order to make sure the lists are the same length I append zeros in each run.

As soon as I set a condition so two lists overlap I get a gap the "size" of the desired overlap after the third iteration eventhough it should append directly to the corresponding list. Additionally if i set several overlaps (e.g. for each iteration) they add up so e.g. for three overlaps each the size of two i get a gap of six.

Below you see what i mean

 w/o overlap            w overlap (returns)    w overlap (should return)
   1  0  0                    1  0  0                   1  0  0
   1  0  0                    1  0  0                   1  0  0
   0  1  0                    0  1  0                   0  1  0
   0  1  0                    0  1  1                   0  1  1
   0  1  0                    0  1  1                   0  1  1
   0  0  1                    0  0  1                   0  0  1
   0  0  1                    0  0  0                   1  0  0
   0  0  1                    0  0  0                   1  0  0 
   1  0  0                    1  0  0                   
   1  0  0                    1  0  0                   

I have created a Pyfiddle here with the code I am using (it is the shortest I could create).It should give you the idea of what I am trying to achieve and what I have done so far.

Further I have used this post to wrap my head around it but it does not seem to apply to my problem.

EDIT: I think I have narrowed down the problem. It seems that due to the overlap the relevant list is being "pulled up" by the size of the overlap without adjusting the size of the remaining lists by the size of the offset. So the difference is being filled by zeros.

EDIT2: My idea is to add the overlap/offset before the list is filled depending on the size of its predecessor. Since the start index depends on the the size of the predecessor it could be calculated using the difference of predecessor size and the gap.

Basically in the parent for-loop for i in range(len(data_j)) I would add:

        overlap = len(data_j[i-1]['axis'])-offset

Unfortunately another problem occured during the process which you can find here Connect string value to a corresponding variable name


Solution

  • I have solved it using the steps from teh other post regarding this issue (See here: Connect string value to a corresponding variable name)

    I have created another fiddle with the solution so you can compare it with the original fiddle to see what I did.

    New Fiddle

    Bascially I add the offset by summing up the size of the current predecessor list and the offset value (which can be negative as well to create an overlap). This sum is assigned to n_offset. Further another problem occurred with .append.

    As soon as all lists are filled and you need to append more values to one of these lists the gap occurs again. This is caused by the for-loop appending the zeros. The range is n_offset and since it takes the size of the predecessor-list it just adds an amount of zeros the size of the first filling of the same list. That's why you have to subtract the length of the list from n_offset.