如何将 List2 拆分为 3 个列表,
首先列出 4 项
第二个清单 4 项
第三个列表 2 项
public static class ListExtensions
{
public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
}
double[][] x;
x = new double[10][];
for (int ii = 0; ii < 10; ii++)
{
x[ii] = new double[4];
for (int jj = 0; jj < 4; ++jj)
x[ii][jj] = 0.45;
}
// Convert x matrix to list
List<double[][]> List2= new List<double[][]>();
List2.Add(x);
List<double[][]> List1 = new List<double[][]>();
List1 = ListExtensions.ChunkBy(List2, 3)[0]; // must be List of double[][]
Console.Write(List1.Count ); // it should be 3
Console.ReadLine();
List1 应该包括 3 个 double[][] 列表,但我有一个列表,
List1.Count : 1
List1 应该是这样的:
List1[0] = x[0], x[1], x[2], x[3]
List1[1] = x[4], x[5], x[6], x[7]
List2[2] = x[8], x[9]
我希望这是您需要的功能:
List<double[][]> ChunkBy(double[][] x, int number){
var result = new List<double[][]>();
int chunkSize = (int)Math.Ceiling(((double)x.GetLength(0))/number);
for(int i = 0; i< number; i++){
result.Add(x.Skip(chunkSize * i).Take(chunkSize).ToArray());
}
return result;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句