嵌套循环中缺少坐标

时间:2015-12-16 18:00:47

标签: python loops

grid=400
P1=[]

for i in range(grid+1):
    P1.append(i/grid)

x=[]
y=[]
z=[]

for j in range(len(P1)):
    for k in range(len(P1)):
        for l in range(len(P1)):
            if P1[j] + P1[k] + P1[l] == 1.0:
                x.append(P1[j])
                y.append(P1[k])
                z.append(P1[l])

我正在制作一个三元网格,我注意到有些坐标丢失了。我很确定条件是正确的,当我用不同的网格大小构建

时,我会得到随机坐标缺失

例如,在400网格尺寸中,缺少坐标的示例如下: -

....
0.5025,0.29,0.2075
0.5025,0.2975,0.2
0.5025,0.3,0.1975
....
缺少

(0.5025,0.2925,0.205)和(0.5025,0.295,0.2025)。有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

import itertools

grid=400
P1=[i/grid for i in range(grid+1)]
epsilon = 0.001

x=[]
y=[]
z=[]
coords = [x,y,z]

for vals in itertools.product(P1, P1, P1)
    if abs(sum(vals)-1) >= epsilon: continue  # this epsilon value will correct for the variations due to floating point arithmetic
    for L,v in zip(coords, vals): L.append(v)