Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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/wordpress/13.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语言中将一个int数组拆分为几个int数组的列表#_C#_Python_Arrays - Fatal编程技术网

C# 在C语言中将一个int数组拆分为几个int数组的列表#

C# 在C语言中将一个int数组拆分为几个int数组的列表#,c#,python,arrays,C#,Python,Arrays,我必须将python语言转换为C#一些代码,其中一部分我遇到了困难 def split_data(seq, length): return [seq[i:i + length] for i in range(0, len(seq), length)] print(split_data([4,5,6,8,5],2)) 这段代码的目的是在参数中给出一个int数组,并将其拆分为length参数的数组。例如,这里的打印结果将是:[[4,5],[6,8],[5]] 问题是我需要在C#中有同样的

我必须将python语言转换为C#一些代码,其中一部分我遇到了困难

def split_data(seq, length):
    return [seq[i:i + length] for i in range(0, len(seq), length)]

print(split_data([4,5,6,8,5],2))
这段代码的目的是在参数中给出一个int数组,并将其拆分为length参数的数组。例如,这里的打印结果将是:
[[4,5],[6,8],[5]]

问题是我需要在C#中有同样的东西。 所以我开始创建一个
列表
。我知道如何在其中添加int[],但我不知道如何像在Python中那样拆分它们,特别是使用这个长度参数

我尝试使用for、foreach循环甚至IEnumerable来实现它,但我无法让它工作

也许有一个非常简单的方法来完成它或者一些我还没有注意到的事情。我对C#的低知识也帮不了我:)


无论如何,谢谢你的帮助。

这应该可以。它是泛型的,所以它应该适用于任何数组,无论它是哪种类型

/// <summary>
/// Splits an array into sub-arrays of a fixed length. The last entry will only be as long as the amount of elements inside it.
/// </summary>
/// <typeparam name="T">Type of the array</typeparam>
/// <param name="array">Array to split.</param>
/// <param name="splitLength">Amount of elements in each of the resulting arrays.</param>
/// <returns>An array of the split sub-arrays.</returns>
public static T[][] SplitArray<T>(T[] array, Int32 splitLength)
{
    List<T[]> fullList = new List<T[]>();
    Int32 remainder = array.Length % splitLength;
    Int32 last = array.Length - remainder;
    for (Int32 i = 0; i < array.Length; i += splitLength)
    {
        // Get the correct length in case this is the last one
        Int32 currLen = i == last ? remainder : splitLength;
        T[] currentArr = new T[currLen];
        Array.Copy(array, i, currentArr, 0, currLen);
        fullList.Add(currentArr);
    }
    return fullList.ToArray();
}
//
///将数组拆分为固定长度的子数组。最后一个条目的长度仅与其中的元素数量相同。
/// 
///数组的类型
///要拆分的数组。
///每个结果数组中的元素数量。
///拆分子数组的数组。
公共静态T[]SplitArray(T[]array,Int32 splitLength)
{
List fullList=新列表();
Int32余数=数组。长度%splitLength;
Int32 last=array.Length-余数;
for(Int32 i=0;i
这里有一个使用
收益回报率的解决方案:

public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> seq, int length) {
    // figure out how many little sequences we need to create
    int count = seq.Count();
    int numberOfTimes = count % length == 0 ? count / length : count / length + 1;

    for (int i = 0 ; i < numberOfTimes ; i++) {
        yield return seq.Take(length);
        seq = seq.Skip(length);
    }
}
其他副本的可能副本:和。在提问之前,请再次访问并特别注意以下内容:研究-谢谢。
new int[] {1,2,3,4,5,6,7}.Split(2)