在List <list <double []>&gt;

时间:2015-07-03 13:25:11

标签: c# linq list

我正在努力尝试以特定方式合并列表列表。我想做的事情如下所述。

我的输入列表是:

列出A = List<List<double[]>>

列出B = List<int[]>

所需的输出是:

列出C = List<List<double[]>>

我希望合并列表A中的一些List<double[]>(例如,使用linq中的Union功能),并将结果列表存储在名为“ListC”的List<List<double[]>>中。

要在ListA中合并的列表的索引存储在ListB中。如果不应该合并元素(它们的索引不会出现在ListB中),那么我们将它们直接添加到ListC。

示例

输入

ListA = new List<List<double[]>>() { a0, a1, a2, a3, a4, a5}; 

其中ai = List<double[]>

ListB = new List<int[]>() { new int[]{0,1,2}, new int[]{3,5} }; 

表示必须将列表0-1-2合并为一个。与清单3-5相同

输出:ListC = List<List<double[]>>包含所有已合并的List<double[]>以及列表A的所有唯一List<double[]>。即:

- &GT; c0 = a0,a1,a2的并集“要合并为1的三个列表”

- &GT; c1 = a3的联合,a5“两个列表合并为2”

- &GT; c2 = a4“是一个唯一的列表”

注意:重复不是问题。

实现这一目标的好方法是什么?

代码:

using System;
using System.Collections.Generic;
using System.Linq;


class _unionLists
{

    public static void Main(string[] args)
    {

        #region input lists
        //--------------------LIST A---------------------------------------------------
        //List a0
        var point1 = new double[] { 3, 3, 0 };
        var point2 = new double[] { 9, 6, 0 };
        var point3 = new double[] { 12, 8, 0 };
        var point4 = new double[] { 18, 8, 0 };
        List<double[]> a0 = new List<double[]>() { point1, point2, point3, point4 };

        //List a1
        var point5 = new double[] { 3, 3, 0 };
        var point6 = new double[] { 9, 7, 0 };
        var point7 = new double[] { 15, 7, 0 };
        var point8 = new double[] { 21, 15, 0 };
        List<double[]> a1 = new List<double[]>() { point5, point6, point7, point8 };

        //List a2
        var point9 = new double[] { 20, 13, 0 };
        var point10 = new double[] { 22, 16, 0 };
        List<double[]> a2 = new List<double[]>() { point9, point10 };

        //List a3
        var point11 = new double[] { 15, 19, 0 };
        var point12 = new double[] { 27, 19, 0 };
        List<double[]> a3 = new List<double[]>() { point11, point12 };

        //List a4
        var point13 = new double[] { 18, 20, 0 };
        var point14 = new double[] { 21, 19, 0 };
        List<double[]> a4 = new List<double[]>() { point13, point14 };

        //List a5
        var point15 = new double[] { 27, 19.5, 0 };
        var point16 = new double[] { 30, 5, 0 };
        List<double[]> a5 = new List<double[]>() { point15, point16 };          

        var ListA = new List<List<double[]>>() { a0, a1, a2, a3, a4, a5};

        //--------------------LIST B---------------------------------------------------
        var ListB = new List<int[]>() { new int[]{0,1,2} , new int[]{3,5} };

        #endregion

        //There are some of the List<double[]> within List A that I want to merge (e.g. using  the Union capability in linq) and store the resultant list in a List<List<double[]>> called "ListC".
        //The indices of the Lists to be merged within ListA are stored in the ListB. If the elements are not suposed to be merged (their index doesn't appear in ListB) then we add them directly to ListC.
        //
        //Example: 
        //  Inputs: ListA = new List<List<double[]>>() { a0, a1, a2, a3, a4, a5}; where ai=List<double[]>;
        //          ListB = new List<int[]>() { new int[]{0,1,2} , new int[]{3,5} }; indicates that the lists 0-1-2 have to be merged into one. Same with lists 3-5
        //  Output: ListC= List<List<double[]>> contains all the merged List<double[]> as well as all the unique List<double[]> of List A. That is:
        //          c0= union of a0, a1, a2 "three lists to be merged into 1"
        //          c1= union of a3, a5 "two lists to be merged into 2"
        //          c2= a4 "is a unique list"

        var ListC = new List<List<double[]>>();


    }
}

3 个答案:

答案 0 :(得分:2)

试试这个:

var elementsACount = ListA.Count();
var groupedElementsIdxs = ListB.SelectMany(e => e);
var ungroupedElementsIdxs = Enumerable.Range(0, elementsACount).Except(groupedElementsIdxs);

var result = new List<List<double[]>>();
// Merge and add the grouped elements.
foreach (var el in ListB)
{
    result.Add(el.Select(e => ListA[e]).SelectMany(e => e).ToList());
}
// Merge and add the ungrouped elements.
result.Add(ungroupedElementsIdxs.Select(e => ListA[e]).SelectMany(e => e).ToList());

答案 1 :(得分:0)

var result = new List<List<double[]>>();

foreach (var array in listB)
{
    var ld = new List<double[]>();
    foreach (var index in array)
    {
        ld.Concat(listA[index]);
    }

    result.Add(ld);
}

var indices = listB.SelectMany(arr => arr);

result.AddRange(listA.Where((list, index) => !indices.Contains(index)));

答案 2 :(得分:-1)

你可以写

var C = A.SelectMany(x => x).Union(B);