如何"压缩"或"旋转"可变数量的列表?

Mic*_*x2a 13 c# linq algorithm

如果我有一个包含任意数量列表的列表,如下所示:

var myList = new List<List<string>>()
{
    new List<string>() { "a", "b", "c", "d" },
    new List<string>() { "1", "2", "3", "4" },
    new List<string>() { "w", "x", "y", "z" },
    // ...etc...
};
Run Code Online (Sandbox Code Playgroud)

...有没有办法以某种方式将列表"压缩"或"旋转"成这样的东西?

{ 
    { "a", "1", "w", ... },
    { "b", "2", "x", ... },
    { "c", "3", "y", ... },
    { "d", "4", "z", ... }
}
Run Code Online (Sandbox Code Playgroud)

显而易见的解决方案是做这样的事情:

public static IEnumerable<IEnumerable<T>> Rotate<T>(this IEnumerable<IEnumerable<T>> list)
{
    for (int i = 0; i < list.Min(x => x.Count()); i++)
    {
        yield return list.Select(x => x.ElementAt(i));
    }
}

// snip

var newList = myList.Rotate();
Run Code Online (Sandbox Code Playgroud)

...但我想知道是否有更清洁的方式,使用linq或其他方式?

Eri*_*ert 18

您可以滚动自己的ZipMany实例,手动迭代每个枚举.对于较大的序列,这可能比GroupBy在投射每个序列后使用的序列表现更好:

public static IEnumerable<TResult> ZipMany<TSource, TResult>(
    IEnumerable<IEnumerable<TSource>> source,
    Func<IEnumerable<TSource>, TResult> selector)
{
   // ToList is necessary to avoid deferred execution
   var enumerators = source.Select(seq => seq.GetEnumerator()).ToList();
   try
   {
     while (true)
     {
       foreach (var e in enumerators)
       {
           bool b = e.MoveNext();
           if (!b) yield break;
       }
       // Again, ToList (or ToArray) is necessary to avoid deferred execution
       yield return selector(enumerators.Select(e => e.Current).ToList());
     }
   }
   finally
   {
       foreach (var e in enumerators) 
         e.Dispose();
   }
}
Run Code Online (Sandbox Code Playgroud)


Sim*_*ger 12

您可以使用Select扩展程序执行此操作Func<T, int, TOut>:

var rotatedList = myList.Select(inner => inner.Select((s, i) => new {s, i}))
                        .SelectMany(a => a)
                        .GroupBy(a => a.i, a => a.s)
                        .Select(a => a.ToList()).ToList();
Run Code Online (Sandbox Code Playgroud)

这会给你另一个List<List<string>>.

分解

.Select(inner => inner.Select((s, i) => new {s, i}))
Run Code Online (Sandbox Code Playgroud)

对于每个内部列表,我们将列表的内容投影到具有两个属性的新匿名对象:s字符串值,以及i原始列表中该值的索引.

.SelectMany(a => a)
Run Code Online (Sandbox Code Playgroud)

我们将结果展平为一个列表

.GroupBy(a => a.i, a => a.s)
Run Code Online (Sandbox Code Playgroud)

我们按i匿名对象的属性进行分组(回想一下这是索引)并选择s属性作为我们的值(仅限字符串).

.Select(a => a.ToList()).ToList();
Run Code Online (Sandbox Code Playgroud)

对于每个组,我们将可枚举更改为列表,并将所有组的另一个列表更改为列表.

  • +1,但我认为OP的原始方法*方式*更具可读性,因此我可能在此处占少数. (2认同)

use*_*116 5

如何使用SelectManyGroupBy使用一些索引?

// 1. Project inner lists to a single list (SelectMany)
// 2. Use "GroupBy" to aggregate the item's based on order in the lists
// 3. Strip away any ordering key in the final answer
var query = myList.SelectMany(
    xl => xl.Select((vv,ii) => new { Idx = ii, Value = vv }))
       .GroupBy(xx => xx.Idx)
       .OrderBy(gg => gg.Key)
       .Select(gg => gg.Select(xx => xx.Value));
Run Code Online (Sandbox Code Playgroud)

来自LinqPad:

我们分组达物品