[英]Porting, Python to C# - How can I iterate a tuple over a tuple?
我正在將 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])
{
[...]
}
}
所以現在我很困惑,很多錯誤,這里有。 我似乎過於復雜和不切實際。
我希望有人可以幫助我:)
如果您需要更多信息,只需發表評論,如果難以閱讀,再次發表評論。
~酷q
這是你可以去做的一種方式......
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 })
{
//[...]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.