c# - 如何使用 LINQ 将列表垂直拆分为 n 个部分

标签 c# linq list

我想将列表分成几部分,不知道该列表中会有多少项。这个问题与那些想要将列表拆分为固定大小的 block 的问题不同。

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

我希望值垂直拆分

一分为二:

-------------------
| item 1 | item 6 |
| item 2 | item 7 |
| item 3 | item 8 |
| item 4 | item 9 |
| item 5 |        |

一分为三:

| item 1 | item 4 | item 7 |
| item 2 | item 5 | item 8 |
| item 3 | item 6 | item 9 |

分成 4 份:

| item 1 | item 4 | item 6 | item 8 |
| item 2 | item 5 | item 7 | item 9 |
| item 3 |        |        |        |

我发现了一些可以做到这一点的 c# 扩展,但它没有按照我想要的方式分配值。这是我发现的:

// this technic is an horizontal distribution
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
    int i = 0;
    var splits = from item in list
                    group item by i++ % parts into part
                    select part.AsEnumerable();
    return splits;
}

结果是这样的,但我的问题是值水平分布:

| item 1 | item 2 |
| item 3 | item 4 |
| item 5 | item 6 |
| item 7 | item 8 |
| item 9 |        |

| item 1 | item 2 | item 3 |
| item 4 | item 5 | item 6 |
| item 7 | item 8 | item 9 |

知道如何垂直分布我的值并有可能选择我想要的部分数量吗?

在现实生活中

对于那些想知道在哪种情况下我想垂直拆分列表的人,这里是我网站的一部分的屏幕截图:

enter image description here

最佳答案

.Take().Skip()你可以:

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int splitIndex = 4; // or (a.Length / 2) to split in the middle.

var list1 = a.Take(splitIndex).ToArray(); // Returns a specified number of contiguous elements from the start of a sequence.
var list2 = a.Skip(splitIndex).ToArray(); // Bypasses a specified number of elements in a sequence and then returns the remaining elements.

您可以使用 .ToList()而不是 .ToArray()如果你想要 List<int> .


编辑:

在您稍微更改(可能澄清)您的问题后,我想这就是您需要的:

public static class Extensions
{
    public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int parts)
    {
        var list = new List<T>(source);
        int defaultSize = (int)((double)list.Count / (double)parts);
        int offset = list.Count % parts;
        int position = 0;

        for (int i = 0; i < parts; i++)
        {
            int size = defaultSize;
            if (i < offset)
                size++; // Just add one to the size (it's enough).

            yield return list.GetRange(position, size);

            // Set the new position after creating a part list, so that it always start with position zero on the first yield return above.
            position += size;
        }
    }
}

使用它:

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var lists = a.Split(2);

这会产生:

一分为二:a.Split(2);

| item 1 | item 6 |
| item 2 | item 7 |
| item 3 | item 8 |
| item 4 | item 9 |
| item 5 |        |

分成 3 部分:a.Split(3);

| item 1 | item 4 | item 7 |
| item 2 | item 5 | item 8 |
| item 3 | item 6 | item 9 |

分成 4 个:a.Split(4);

| item 1 | item 4 | item 6 | item 8 |
| item 2 | item 5 | item 7 | item 9 |
| item 3 |        |        |        |

另外,如果你有:

int[] b = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // 10 items

并分成 4 份:b.Split(4);

| item 1 | item 4 | item 7 | item 9 |
| item 2 | item 5 | item 8 | item 10|
| item 3 | item 6 |        |        |

关于c# - 如何使用 LINQ 将列表垂直拆分为 n 个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13502556/

相关文章:

apache-flex - Flex 4 Spark List 设置滚动位置

C# Mysql 更新表中的最后一行

C# COM 对象 VBA 函数或接口(interface)标记为受限或函数使用不受支持的自动化类型

c# - 我可以结合使用 LINQ 和 foreach 来将对象添加到集合中吗?

PHP:如何列出目录中的文件而不列出子目录

python - 如何在一行中以空格分隔输入列表中的元素?

c# - 如何使用 mvc 4 向我的 api 添加 token ?

c# - 使用 linq 将 json 字符串解析为对象

linq - 如何在 lambda 表达式中调用方法?

c# - 如何在 LINQ WHERE 语句中使用 OR 运算符