2016-08-28 22 views
0

我将一个Python项目移植到C#。移植,Python到C# - 如何迭代元组上的元组?

到目前为止,我遇到了这个问题,有没有什么办法可以将此端口移植到C#?

verts = (-1,-1,-1),(1,-1,-1),(1,1,-1),(-1,1,-1),(-1,-1,1),(1,-1,1),(1,1,1),(-1,1,1) 
edges = (0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),(0,4),(1,5),(2,6),(3,7) 

for edge in edges: 
    for x,y,z in (verts[edge[0]],verts[edge[1]]): 
     [...] 

我试过这个,

verts.Add(new List<string> { "-1,-1,-1" }); 
verts.Add(new List<string> { "1,-1,-1" }); 
verts.Add(new List<string> { "1,1,-1" }); 
verts.Add(new List<string> { "-1,1,-1" }); 
verts.Add(new List<string> { "-1,-1,1" }); 
verts.Add(new List<string> { "1,-1,1" }); 
verts.Add(new List<string> { "1,1,1" }); 
verts.Add(new List<string> { "-1,1,1" }); 

edges.Add(new List<string> { "0,1" }); 
edges.Add(new List<string> { "1,2" }); 
edges.Add(new List<string> { "2,3" }); 
edges.Add(new List<string> { "3,0" }); 
edges.Add(new List<string> { "4,5" }); 
edges.Add(new List<string> { "5,6" }); 
edges.Add(new List<string> { "6,7" }); 
edges.Add(new List<string> { "7,4" }); 
edges.Add(new List<string> { "0,4" }); 
edges.Add(new List<string> { "1,5" }); 
edges.Add(new List<string> { "2,6" }); 
edges.Add(new List<string> { "3,7" }); 

foreach (List<string> vert in verts) 
     { 

      [...] 
     } 

     List<string> lines1 = new List<string>(); 
     List<string> lines2 = new List<string>(); 

     foreach (List<string> edge in edges) 
     { 
      int edge1 = int.Parse(edge[0].Split(',')[0]); 
      int edge2 = int.Parse(edge[0].Split(',')[1]); 

      int x; 
      int y; 
      int z; 

      foreach (int vert in verts[edge1]) 
      { 
       [...] 
      } 
     } 

所以现在我正在很困惑,很多的错误,在这里任何那里。 我似乎过于复杂和不切实际。

我希望有人能帮助我:)

如果您需要了解更多信息,只是发表评论,如果它是很难读,目的也只是发表评论。

〜Coolq

回答

1

这是一个你可以去这样做,从我的方式...

 var verts = new[] 
     { 
      new Tuple<int,int,int> (-1,-1,-1), 
      new Tuple<int,int,int> (1,-1,-1), 
      new Tuple<int,int,int> (1,1,-1), 
      new Tuple<int,int,int> (-1,1,-1), 
      new Tuple<int,int,int> (-1,-1,1), 
      new Tuple<int,int,int> (1,-1,1), 
      new Tuple<int,int,int> (1,1,1), 
      new Tuple<int,int,int> (-1,1,1) 
     }; 
     var edges = new[] 
     { 
      new Tuple<int,int>(0,1), 
      new Tuple<int,int>(2,2), 
      new Tuple<int,int>(2,3), 
      new Tuple<int,int>(3,0), 
      new Tuple<int,int>(4,5), 
      new Tuple<int,int>(5,6), 
      new Tuple<int,int>(6,7), 
      new Tuple<int,int>(7,4), 
      new Tuple<int,int>(0,4), 
      new Tuple<int,int>(1,5), 
      new Tuple<int,int>(2,6), 
      new Tuple<int,int>(3,7) 
     }; 

     foreach(var edge in edges) 
     { 
      var edge1 = edge.Item1; 
      var edge2 = edge.Item2; 

      int x, y, z;//not sure why you need these? 

      foreach(var vert in new[] { verts[edge1].Item1, verts[edge1].Item2, verts[edge1].Item3 }) 
      { 
       //[...] 
      } 
     } 
+0

很显然你已经把时间和精力投入到这一点,+1 :) 它也非常干净清晰。所以这也是一个检查;) –