嵌套列表重叠会产生不必要的间隙 [英] Overlap of nested lists creates unwanted gap

查看:104
本文介绍了嵌套列表重叠会产生不必要的间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套,其中包含三个由for循环填充的列表,并且填充由if条件控制.

第一次迭代后,它可能类似于以下示例:

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

,根据条件,它们不重叠.第二次迭代后,会将新值附加到相应的嵌套列表中.为了确保列表的长度相同,我在每次运行中都添加零.

一旦我设置了一个条件,使两个列表重叠,即使应该直接附加到相应的列表上,我也会在第三次迭代后得到所需重叠的大小"的缺口.另外,如果我设置了几个重叠(例如每次迭代),它们会加起来,例如对于三个重叠部分,每个重叠部分的大小为两个,则我的差距为六个.

在下面您明白我的意思

 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创建了一个 Pyfiddle 正在使用(这是我能创建的最短的时间).它应该使您了解我正在努力实现的目标以及到目前为止所做的一切.

此外,我已将此 post 用于缠住我的头,但这似乎不适用于我的问题.

编辑:我认为我已经缩小了问题的范围.似乎由于重叠,相关列表被重叠的大小拉起",而没有通过偏移量的大小调整其余列表的大小.因此差异被零填充.

我的想法是在填充列表之前添加重叠/偏移量,具体取决于其前身的大小.由于起始索引取决于前任对象的大小,因此可以使用前任成员的大小和空白之差来计算.

基本上在父循环for i in range(len(data_j))中,我会添加:

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

不幸的是,在此过程中发生了另一个问题,您可以在这里找到连接字符串值转换为相应的变量名

解决方案

我已使用其他帖子中有关此问题的步骤解决了此问题(请参见此处:新小提琴

基本上,我通过将当前前任列表的大小和偏移值(也可以为负值来创建重叠)相加来添加偏移量.此和分配给n_offset.另外,.append发生了另一个问题.

所有列表填充完毕后,您需要向这些列表之一添加更多值,然后再次出现间隙.这是由for-loop附加零引起的.范围为n_offset,由于它采用了前一个列表的大小,因此它只增加了零(即同一列表的第一次填充的大小).这就是为什么您必须从n_offset中减去列表的长度.

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

解决方案

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.

这篇关于嵌套列表重叠会产生不必要的间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆