【发布时间】:2015-09-21 08:40:20
【问题描述】:
我正在努力尝试以特定方式合并列表列表。我想要做的如下所述。
我的输入列表是:
列表 A = List<List<double[]>>
列表 B = List<int[]>
而期望的输出是:
列表 C = List<List<double[]>>
我想要合并列表 A 中的一些 List<double[]>(例如,使用 linq 中的联合功能)并将结果列表存储在名为“ListC”的 List<List<double[]>> 中。
要在 ListA 中合并的 List 的索引存储在 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 A所有合并的List<double[]>以及唯一的List<double[]>。即:
--> c0= union of a0, a1, a2 "三个要合并为1的列表"
--> c1= union of 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[]>>();
}
}
【问题讨论】:
-
您实际尝试过什么?您看起来好像在这里拥有所有设置代码,但我看不出您实际上在哪里尝试进行合并?你的问题到底出在哪里?你能想出一个算法来做到这一点吗?您是否有无法实现的算法?还有什么?
-
创建一个更准确地表示您的数据的数据结构可能是值得的。我不确定这是否应该是矩阵或多维数组或其他东西,但是如果您编写了一个类来表示它,那将更容易考虑需要对数据应用哪些转换。
-
@Asad 同意 - 人脑不擅长跟踪嵌套数据结构,最好给它们赋予一些含义