简体   繁体   English

将两个锯齿状列表合并为一个

[英]Combine two jagged lists into one

Im having two jagged lists. 我有两个参差不齐的列表。

First one: 第一:

List<List<string>> firstList = new List<List<string>>();

{ dd ff }
{ dd ff }
{ dd ff }

Second one: 第二个:

List<List<string>> secondList = new List<List<string>>();

{ gg hh }
{ gg uu }
{ hh uu }

Im having a problem with combining both lists into one like this one: 我在将两个列表组合成这样的列表时遇到问题:

{ dd ff gg hh }
{ dd ff gg uu }
{ dd ff hh uu }

Any Concatenation attempt resulted in just adding secondList elements like another firstList rows like this: 任何串联尝试都只会添加secondList元素,例如另一个firstList行,如下所示:

{ dd ff }
{ dd ff }
{ dd ff }
{ gg hh }
{ gg uu }
{ hh uu }

Would appreciate any help with this one! 希望对此有任何帮助!

You can using Zip to do that to concatenate the corresponding sub-lists. 您可以使用Zip来连接相应的子列表。

var combined = firstList.Zip(secondList, (f,s) => f.Concat(s).ToList()).ToList();

Note that if the two lists contain a different number of sub-lists the result will only have as many as the shorter of the two lists. 请注意,如果两个列表包含不同数量的子列表,则结果将仅具有两个列表中较短者的数量。

You can use Zip extension method that is a part of the System.Linq namespace. 您可以使用System.Linq命名空间的一部分的Zip扩展方法。 Tuples can be used to associate values form both lists. 元组可用于关联两个列表中的值。 Or you can just Concat inner lists. 或者,您也可以仅Concat内部列表。

var list1 = new List<List<string>>
{
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
};

var list2 = new List<List<string>>
{
    new List<string> { "gg", "hh" },
    new List<string> { "gg", "uu" },
    new List<string> { "hh", "uu" },
};

var result = list1.Zip(list2, (l1, l2) => Tuple.Create(l1, l2)).ToList();

The important idea here is you have to concat each element of the list rather than concatting the two lists. 这里的重要思想是必须合并列表中的每个元素 ,而不是合并两个列表。

  1. Concat each element: 连接每个元素:

     { dd ff gg hh } //each element dd ff is concatted with gg hh { dd ff gg uu } { dd ff hh uu } 
  2. Concat the lists: 合并列表:

     { dd ff } { dd ff } { dd ff } //elements from the 1st list ----------- concated with { gg hh } { gg uu } { hh uu } //elements from the 2nd list 

You could Concat each element of the lists (which have to have same number of elements or the second one more number than the first) like this: 您可以Concat列表中的每个元素(必须具有相同数量的元素,或者第二个元素要比第一个元素 ),如下所示:

List<List<string>> finalList = (from x in firstList
                                join y in secondList
                                on firstList.IndexOf(x) equals secondList.IndexOf(y)
                                select x.Concat(y).ToList()).ToList();

Or any other way to feel like to do it. 或任何其他想做的方式。

Another option is just loop through the first list and add the elements from the second list. 另一种选择是循环遍历第一个列表并添加第二个列表中的元素。 For example: 例如:

var list1 = new List<List<string>> {
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" }};

var list2 = new List<List<string>> {
    new List<string> { "gg", "hh" },
    new List<string> { "gg", "uu" },
    new List<string> { "hh", "uu" }};

for(var j = 0; j < list1.Count(); j++)
{
    list1[j].AddRange(list2[j]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM