合并某些列表<;double[]>;在列表<;列表<;double[]>>

本文关键字:gt lt 列表 double 合并 | 更新日期: 2023-09-27 18:00:54

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

我的输入列表是:

列表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[]>>包含列表A的所有合并的List<double[]>以及所有唯一的List<double[]>。即:

-->c0=a0、a1、a2"三个列表合并为1"的并集

-->c1=a3,a5"两个列表合并为2"的并集

-->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[]>>();

    }
}

合并某些列表<;double[]>;在列表<;列表<;double[]>>

试试这个:

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());
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)));

您可以编写

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