如何在多重for循环中更新列表?

时间:2019-02-08 17:21:43

标签: python python-3.x list loops

我想制作一个网格状的结构,并愿意检查地理位置是否在单元格中?

我具有如下所示的元组坐标,也有其他坐标作为列表的列表,其中还包含一些坐标,每个子列表中坐标的数量不同。我想要检查元组中的子列表的每个坐标与地理坐标。结果,我想要一个值为1或0(基于给定条件)的坐标子列表的元组大小列表。如果一个坐标的子列表的坐标在data1的坐标范围内,则写1否则为0。

如果我从坐标的子列表中选择一个地理点(6.8,51.35),我将检查每个元组的地理坐标,就像a =(x,y)将其修改为(z = x + 0.49,p = y +0.49),0.49(因为像元间隔为0.05),然后将值(6.8,51.35)与(a,b)和(z,p)范围进行比较,将值(x,y)替换为1,否则为0。

Tuple = ((6.8, 51.35), (6.85, 51.4), (6.9, 51.45), (6.95, 51.5), (7.0, 51.55), (7.05, 51.6), (7.1, 51.65), (7.15, 51.7), (7.2, 51.75), (7.25, 51.8), (7.3, 51.85), (7.35, 51.9), (7.4, 51.95), (7.45, 52.0), (7.5, 52.05), (7.55, 52.1), (7.6, 52.15), (7.65, 52.2), (7.7, 52.25), (7.75, 52.3), (7.8, 52.35), (7.85, 52.4), (7.9, 52.45), (7.95, 52.5), (8.0, 52.55), (8.05, 52.6), (8.1, 52.65), (8.15, 52.7), (8.2, 52.75), (8.25, 52.8), (8.3, 52.85), (8.35, 52.9), (8.4, 52.95), (8.45, 53.0), (8.5, 53.05), (8.55, 53.1), (8.6, 53.15), (8.65, 53.2), (8.7, 53.25), (8.75, 53.3), (8.8, 53.35), (8.85, 53.4), (8.9, 53.45), (8.95, 53.5), (9.0, 53.55), (9.05, 53.6), (9.1, 53.65), (9.15, 53.7), (9.2, 53.75), (9.25, 53.8), (9.3, 53.85), (9.35, 53.9), (9.4, 53.95), (9.45, 54.0), (9.5, 54.05), (9.55, 54.1), (9.6, 54.15), (9.65, 54.2), (9.7, 54.25), (9.75, 54.3), (9.8, 54.35), (9.85, 54.4), (9.9, 54.45))

coordinate = [[('9.72', '52.33071'), ('9.72005', '52.3306')],[('9.7909166', '52.353843'), ('9.7907', '52.35399'), ('9.7904', '52.35417'), ('9.7897', '52.35454'), ('9.7891696', '52.354815')]]

我写的代码是

for i in range(0,2):
    list=[]
    point=cordinate[i]
    y=len(cordinate[i])
    print(y)
    for j in range(0,y):
       l=float(point[j][0])
       m=float(point[j][1])
       #list=[]
       for k in range(0,len(Tuple)):
           x=Tuple[k][0]
           y=Tuple[k][1]
           gap = 0.049
           if (x < l < x + gap) and (y < m < y + gap):
               value = 1
           else:
               value = 0
           list.append(value)
   print(list)

当前输出:

2
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

5
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

2 个答案:

答案 0 :(得分:1)

您有三个问题。前两个是相关的:Vic指出,无论内部if是否为True,都会执行“ else”子句。为了简化检查,请使用

升级您的差距检查代码
gap = 0.049
if (x < l < x + gap) and \
   (y < m < y + gap):
    value = 1
else:
    value = 0

list.append(value)

这也可以解决您的第二个问题:在“发现”的情况下,您将两次附加value:一次在if内,一次之后。

此外,请注意,您可以将支票减少为一项长项:

list.append(int((x < l < x + gap) and (y < m < y + gap)))

最后,您有一个问题,即您的数据在设计上不会产生任何1值。您已经定义了一系列对角线单元。第一个具有(6.8,51.35)和(6.849,51.399)的对角线;下一个在第二个角开始,并沿着网格的对角线继续。

coordinate中的点不属于任何这些单元格。例如,第一点是

(9.72, 52.33071)

包含Tuple坐标的网格(x)点是

(9.7, 54.25), (9.75, 54.3)

但是所需的y坐标在该点之上几乎两个单位(40步)。因此,该点没有匹配的单元格。其他坐标点具有相同的问题。您根本没有符合条件的数据点,因此您的输出列表将全为零。

看来您的数据或问题描述有误。

答案 1 :(得分:0)

Gautam,我同意以上关于MCVE提交的评论。根据发布的代码,您可能想看的地方是您的4参数if语句:

    if l>x and l<z:
      if m>y and m<p:
        value=1
    else : value = 0

您可能会从此代码中获得不需要的输出,因为您的else语句会执行每次迭代。