Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#双重分割列表[][]_C#_List - Fatal编程技术网

C#双重分割列表[][]

C#双重分割列表[][],c#,list,C#,List,如何将列表2拆分为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

如何将列表2拆分为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();
清单1应该是这样的:

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;
}
List ChunkBy(双精度[]]x,整数){
var result=新列表();
int chunkSize=(int)数学上限(((double)x.GetLength(0))/number);
for(int i=0;i
可能重复@RomanKoliada不,这是不同的,我的答案的一部分来自您的链接此操作也称为批处理:@RomanKoliada我的答案的哪一部分不正确?@shdotcom如果您原来的
列表2
只包含一项,那么
列表1
如何包含三项?谢谢您的帮助,但输出不是双重的[][]@shdotcom现在是
double[][]
对不起,我的意思是ListIt's working,非常感谢,但是除了skip,take,是否可以使用其他方法?在这里,他们提到这种方法会消耗内存,这就是为什么我在同一个链接中使用Dmitry Pavlov提供的方法。我认为
GroupBy
在您的情况下并不比“Skip().Take()”更有效
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;
}