wes*_*wes 5 c# python iteration multidimensional-array
在Python中,可以同时迭代多个变量,如下所示:
my_list = [[1, 2, 3], [4, 5, 6]]
for a, b, c in my_list:
pass
Run Code Online (Sandbox Code Playgroud)
有没有比这更接近的C#模拟?
List<List<int>> myList = new List<List<int>> {
new List<int> { 1, 2, 3 },
new List<int> { 4, 5, 6 }
};
foreach (List<int> subitem in myList) {
int a = subitem[0];
int b = subitem[1];
int c = subitem[2];
continue;
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 只是为了澄清,确切的代码是必须为C#示例中的每个索引指定一个名称.
与你所拥有的并没有太大不同,但是这个怎么样?
foreach (var subitem in myList.Select(si => new {a = si[0], b = si[1], c = si[2]})
{
int a = subitem.a;
int b = subitem.b;
int c = subitem.c;
continue;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
446 次 |
| 最近记录: |