3

リストのリストがありますnLedgers-点の3Dクラウド:

[nodeID, X, Y, Z]

複数の行で。一部のノードは、同じ座標XY異なるZ座標を持ちます。

私は、最初に同じ座標を持つ異なる座標を特定したいと思いZます。次に 、最後に についても同じです。XYXY

次に、これらの(X,Y)(X,Z)または(Y,Z)ペアと異なるZYまたはレベルを使用して、2 番目のリスト ( ) のと座標Xを変更したいと思います。xynodes

変更は以下を尊重する必要があります。

1) For x,y,z in `nodes`, if x=X and y=Y and Z level 1 <=z< Z level 2:
   change x and y coordinates between consecutive `Z` levels, for each pair of `(X,Y)` coordinates.

2) For x,y,z in `nodes`, if x=X and Y level 1 <=y< Y level 2:
   change x coordinates between consecutive `Y` levels, for each pair of `(Y,Z)` coordinates.

3) For x,y,z in `nodes`, if y=Y and X level 1 <=x< X level 2:
   change y coordinates between consecutive `X` levels, for each pair of `(X,Z)` coordinates.

どんな助けでも大歓迎です。

以前の投稿に基づいて、次のコードがあります。

from ast import literal_eval
nLedgers=[[literal_eval(x) for x in item] for item in nLedgers] #transform list of strings to list of floats
nodes=[[literal_eval(x) for x in item] for item in nodes]

NewNodesCoord = []
dirX=1 #integer that changes the direction (X,Y) of the vector to change the x and y coordinates
dirY=1-dirX
newy1=0 #float that stores the new y coordinate at x=X1 and z=Z 
newy2=0 #float that stores the new y coordinate at x=X2 (X2>X1) and z=Z 
newx1=0 #float that stores the new x coordinate at y=Y1 and z=Z 
newx2=0 #float that stores the new x coordinate at y=Y2 (Y2>Y1) and z=Z 

from collections import defaultdict
dic1=defaultdict(list) #dictionary of X and Y pairs
dic2=defaultdict(list) #dictionary of X and Z pairs
dic3=defaultdict(list) #dictionary of Y and Z pairs

for id,x,y,z in nLedgers:
    dic1[(x,y)].append((id,z))
for key in dic1:
    dic1[key].sort( key=lambda x:float(x[1]) )

for id,x,y,z in nLedgers:
    dic2[(x,z)].append((id,y))
for key in dic2:
    dic2[key].sort( key=lambda x:float(x[1]) )
#print dic2

for id,x,y,z in nLedgers:
    dic3[(y,z)].append((id,x))
for key in dic2:
    dic3[key].sort( key=lambda x:float(x[1]) )
#print dic3

newNodes=[] #list with changed coordinates
for i,row in enumerate(nodes): #same X,Y, different Z
    id,x,y,z=row
    z_levels=[item[1] for item in dic1[(x,y)]]
    for k,l in zip(z_levels,z_levels[1:]):
        if k<=z<l:
            nodes[i][1]=x+((l-k)/400*sin(pi*(z-k)/l)+max(z_levels)/800*(z/max(z_levels)))*dirX
            nodes[i][2]=y+((l-k)/400*sin(pi*(z-k)/l)+max(z_levels)/800*(z/max(z_levels)))*dirY
            nodes[i][3]=z
            newNodes.append([nodes[i][0], nodes[i][1], nodes[i][2], nodes[i][3]])
#           break 

for i,row in enumerate(nodes):  #same X, different Y
    id,x,y,z=row
    y_levels=[item[1] for item in dic2[(x,z)]]
    z_levels=[item[1] for item in dic1[(x,y)]]
    for k,l in zip(y_levels,y_levels[1:]):
        if x==dic2.keys()[0][0] and k<y<l:
            for m,n in zip(z_levels,z_levels[1:]):
                if m==z and y==k:
                    newx1=x+((n-m)/400*sin(pi*(z-m)/n)+max(z_levels)/800*(z/max(z_levels)))*dirX
                if m==z and y==l:
                    newx2=x+(n-m)/400*sin(pi*(z-m)/n)+max(z_levels)/800*(z/max(z_levels))   
            nodes[i][1]=x+(y-k)*(newx2-newx1)/(l-k)+newx1
            nodes[i][2]=y
            nodes[i][3]=z
            newNodes.append([nodes[i][0], nodes[i][1], nodes[i][2], nodes[i][3]])

for i,row in enumerate(nodes):#same Y, different X
    id,x,y,z=row
    x_levels=[item[1] for item in dic3[(y,z)]]
    z_levels=[item[1] for item in dic1[(x,y)]]
    for k,l in zip(x_levels,x_levels[1:]):
#        print dic3.keys()[0][0]
        if y==dic3.keys()[0][0] and k<x<l: #same X, different Y
            for m,n in zip(z_levels,z_levels[1:]):
                if m==z and y==k:
                    newy1=y+((n-m)/400*sin(pi*(z-m)/n)+max(z_levels)/800*(z/max(z_levels)))*dirY
                if m==z and y==l:
                    newy2=y+((n-m)/400*sin(pi*(z-m)/n)+max(z_levels)/800*(z/max(z_levels)))*dirY  
            nodes[i][1]=x
            nodes[i][2]=y+(y-k)*(newy2-newy1)/(l-k)+newy1
            nodes[i][3]=z
            newNodes.append([nodes[i][0], nodes[i][1], nodes[i][2], nodes[i][3]])

最初の変更 (X、Y ペア) は機能していますが、他の 2 つは機能していません。に問題がありdic2.keys()[0][0]ます。dic2 のキーをループして、最初のキーの値を x と比較したいと思います。

4

2 に答える 2

1

これは、あなたの望むことですか?私は単純な辞書を使用しました..そしてその使用されたタプルをキーとして使用しました

result={}
for x in DataCoord:
    result.setdefault((x[1],x[2]),{})
    result[(x[1],x[2])][tuple(x)]=x[0]

出力:

{('-1000.', '0.'): 
  {('436', '-1000.', '0.', '0.'): '436',
  ('491', '-1000.', '0.', '0.'): '491',
  ('513', '-1000.', '0.', '1000.'): '513',
  ('524', '-1000.', '0.', '2000.'): '524',
  ('535', '-1000.', '0.', '3000.'): '535',
.
.
.

('-1000.', '2000.'): 
  {('818', '-1000.', '2000.', '0.'): '818',
  ('863', '-1000.', '2000.', '0.'): '863',
  ('874', '-1000.', '2000.', '1000.'): '874',
  ('885', '-1000.', '2000.', '2000.'): '885',
 ..
于 2013-04-26T09:37:22.660 に答える