2015-12-16 151 views
0
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)缺失。任何想法來解決這個問題?

+0

不比較== – timgeb

+0

花車考慮使用類似['numpy.meshgrid'](http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html ) – iled

+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) 
+0

thx,我怎樣才能避免類似的問題在未來的任何提示? –

+0

@DannyLee:不要太依賴浮點數學 – inspectorG4dget