如何在没有 class C# 的情况下查找子列表的索引
How to find index of sublist without class C#
我想在列表 route
中找到包含特定值 (i) 的子列表的索引,但我不想创建它的 class。
这是我的代码:
var route = new List<List<int>>();
for (int i = 0; i<DC1; ++i)
{
for (int j = 0; j<DC1; ++j)
{
if (routeopt.x[0, j] == 1)
{
List<int> subroute = new List<int>();
if (routeopt.x[i, j] == 1)
{
subroute.Add(j);
route.Add(subroute);
}
}
}
if(i == 0) continue;
for (int j = 1; j<DC1;j++ )
{
if (routeopt.x[i, j] == 1)
route[route.IndexOf(i)].Add ( j);
}
}
foreach (var subroute in route)
{
Console.Write("subroute: ");
foreach (int s in subroute)
Console.Write(s + " ");
Console.WriteLine();
}
例如,基于此代码:
for (int j = 1; j < DC1;j++ )
{
if (routeopt.x[i, j] == 1)
route[route.IndexOf(i)].Add(j);
}
我想做如果 x[1,3] == 1
那么我可以将 3 添加到包含 1 的子列表中。
此代码 route.IndexOf(i)
仍然是红色下划线,请帮助如何更正它。谢谢
让我们从一个示例开始(稍后我们将其变成测试):
List<List<int>> route = new List<List<int>>() {
new List<int>() {1, 2, 3, 4, 5}, // sublist #0: contains value == 4
new List<int>() {7, 8, 2, 9}, // sublist #1: doesn't contain value == 4
new List<int>() {9, 10, 4}, // sublist #2: contains value == 4
};
我们正在每个子列表中寻找 value
int value = 4;
最后,我们希望将子列表索引作为结果:0
和 2
。
如果是你的情况,我建议使用 Linq
List<List<int>> route = new List<List<int>>() {
new List<int>() {1, 2, 3, 4, 5},
new List<int>() {7, 8, 2, 9},
new List<int>() {9, 10, 4},
};
int value = 4;
var indexesFound = route
.Select((sublist, index) => new { // eh, new class, but anonymous one
sublist = sublist,
index = index, })
.Where(chunk => chunk.sublist.Contains(value))
.Select(chunk => chunk.index)
.ToArray(); // if you want, say, array materialization
测试
Console.Wrire(string.Join(", ", indexesFound));
结果:
0, 2
编辑:如果你只想有一个索引,你必须指定哪一个,例如对于第一个索引,将 .First()
代替 .ToArray()
:
int firstIndexFound = route
.Select((sublist, index) => new { // eh, new class, but anonymous one
sublist = sublist,
index = index, })
.Where(chunk => chunk.sublist.Contains(value))
.Select(chunk => chunk.index)
.First(); // or .Last()
您可以使用 LINQ 的 Single
方法在给定谓词 Contains(i)
的情况下检索您想要的特定列表。在这里我要查找包含 6 的列表,并向其添加 7。
var routes = new List<List<int>>()
{
new List<int>() {1, 2, 3},
new List<int>() {4, 5, 6},
};
List<int> targetList = routes.Single(i => i.Contains(6));
targetList.Add(7);
要具体获取该列表的索引,则可以使用 IndexOf
方法,例如:
int targetListIndex = routes.IndexOf(targetList); // 1 in this example
我想在列表 route
中找到包含特定值 (i) 的子列表的索引,但我不想创建它的 class。
这是我的代码:
var route = new List<List<int>>();
for (int i = 0; i<DC1; ++i)
{
for (int j = 0; j<DC1; ++j)
{
if (routeopt.x[0, j] == 1)
{
List<int> subroute = new List<int>();
if (routeopt.x[i, j] == 1)
{
subroute.Add(j);
route.Add(subroute);
}
}
}
if(i == 0) continue;
for (int j = 1; j<DC1;j++ )
{
if (routeopt.x[i, j] == 1)
route[route.IndexOf(i)].Add ( j);
}
}
foreach (var subroute in route)
{
Console.Write("subroute: ");
foreach (int s in subroute)
Console.Write(s + " ");
Console.WriteLine();
}
例如,基于此代码:
for (int j = 1; j < DC1;j++ )
{
if (routeopt.x[i, j] == 1)
route[route.IndexOf(i)].Add(j);
}
我想做如果 x[1,3] == 1
那么我可以将 3 添加到包含 1 的子列表中。
此代码 route.IndexOf(i)
仍然是红色下划线,请帮助如何更正它。谢谢
让我们从一个示例开始(稍后我们将其变成测试):
List<List<int>> route = new List<List<int>>() {
new List<int>() {1, 2, 3, 4, 5}, // sublist #0: contains value == 4
new List<int>() {7, 8, 2, 9}, // sublist #1: doesn't contain value == 4
new List<int>() {9, 10, 4}, // sublist #2: contains value == 4
};
我们正在每个子列表中寻找 value
int value = 4;
最后,我们希望将子列表索引作为结果:0
和 2
。
如果是你的情况,我建议使用 Linq
List<List<int>> route = new List<List<int>>() {
new List<int>() {1, 2, 3, 4, 5},
new List<int>() {7, 8, 2, 9},
new List<int>() {9, 10, 4},
};
int value = 4;
var indexesFound = route
.Select((sublist, index) => new { // eh, new class, but anonymous one
sublist = sublist,
index = index, })
.Where(chunk => chunk.sublist.Contains(value))
.Select(chunk => chunk.index)
.ToArray(); // if you want, say, array materialization
测试
Console.Wrire(string.Join(", ", indexesFound));
结果:
0, 2
编辑:如果你只想有一个索引,你必须指定哪一个,例如对于第一个索引,将 .First()
代替 .ToArray()
:
int firstIndexFound = route
.Select((sublist, index) => new { // eh, new class, but anonymous one
sublist = sublist,
index = index, })
.Where(chunk => chunk.sublist.Contains(value))
.Select(chunk => chunk.index)
.First(); // or .Last()
您可以使用 LINQ 的 Single
方法在给定谓词 Contains(i)
的情况下检索您想要的特定列表。在这里我要查找包含 6 的列表,并向其添加 7。
var routes = new List<List<int>>()
{
new List<int>() {1, 2, 3},
new List<int>() {4, 5, 6},
};
List<int> targetList = routes.Single(i => i.Contains(6));
targetList.Add(7);
要具体获取该列表的索引,则可以使用 IndexOf
方法,例如:
int targetListIndex = routes.IndexOf(targetList); // 1 in this example