在多维列表的特定行中查找值

本文关键字:查找 列表 | 更新日期: 2023-09-27 18:20:07

我想在C#(List<List<List<double>>>)中查找3D列表的所有值(索引),但只在特定区域中搜索。

在matlab中,这可以通过以下行来完成:

find(my3DList(:,4,:) == 2)

这意味着,搜索第二个维度设置为4的所有位置。我想在C#中使用List做同样的事情。我想FindAll命令可以做到这一点,但我不知道如何告诉程序将第二个维度固定为特定值。

你能帮忙吗?非常感谢

在多维列表的特定行中查找值

您可以使用以下

 var result = list.FindAll(x=>x[4].Contains(6)); // result contains matched elements not indices

这里4在第二维度中是固定的,如果它包含6,它将被检索。

编辑如果你只想要指数,你可以做如下

int[] indices = list.Select((item, index) => new { x = item, i = index })
                    .Where(x => x.x[4].Contains(6))
                    .Select(x => x.i).ToArray();

编辑2用于固定第三维度(此处0是固定的,6是匹配的)

var result = list.Where(x => x.Where(y => y[0] == 6).Count() != 0).ToList();

编辑3用于固定第三维度(这里0是固定的,6是匹配的)并仅获得索引

int[] indices = list.Select((x, i) => new { item = x, index = i })
              .Where(x => x.item.Where(y => y[0] == 6).Count() != 0)
              .Select(x=>x.index).ToArray();

示例

如果列表中的每个元素(外部)都包含第五个(索引=4)元素,则以下内容应该有效。

List<List<List<double>>> list = new List<List<List<double>>>()
{
    new List<List<double>>()
    {
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){6,2,3 },
        new List<double>(){5,2,5 },
    },
    new List<List<double>>()
    {
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){6,2,3 },
        new List<double>(){5,2,5 },
    },
    new List<List<double>>()
    {
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){5,2,3 },
        new List<double>(){5,2,5 },
    },
    new List<List<double>>()
    {
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){1,2,3 },
        new List<double>(){5,2,5 },
        new List<double>(){5,2,3 },
        new List<double>(){5,2,5 },
    }
};
var result = list.FindAll(x=>x[4].Contains(6));
int[] indices = list.Select((item, index) => new { x = item, i = index })
                    .Where(x => x.x[4].Contains(6))
                    .Select(x => x.i).ToArray();

这里result只包含原始列表中的前2个元素,索引包含匹配的索引(0,1)。

假设您有一个三维数据结构,如:

List<List<List<int>>> a = new List<List<List<int>>>()
            {
                new List<List<int>> {
                    new List<int>  {1,2,3},
                    new List<int> {4,5,6},
                    new List<int> {7,8,9},
                },
                new List<List<int>> {
                    new List<int> {10,11,12},
                    new List<int> {13,14,15},
                    new List<int> {16,17,18},
                }
            };

若要将搜索限制在此数据结构中的某个维度,您需要一个使列表变平的辅助方法。

    private static IEnumerable<int> LimitToDimension(IEnumerable<IEnumerable<IEnumerable<int>>> list, int dimension, int index)
    {
        return (dimension == 0 ? list.Where((x, i) => i == index) : list)
            .SelectMany(secondDimension => dimension == 1 ? secondDimension.Where((x, i) => i == index) : secondDimension)
            .SelectMany(thirdDimension => dimension == 2 ? thirdDimension.Where((x, i) => i == index) : thirdDimension);
    }

此方法采用一个3D IEnumerable,一个要筛选的维度和维度索引,并返回该维度和索引中项目的展开列表。例如,调用:

LimitToDimension(a, 1, 1);

返回{4,5,6,13,14,15},然后可以使用"Where"查找所需的元素。此外,您还可以将其转化为一种更易于使用的扩展方法。