How to split a list based on the order of another list

时间:2018-03-23 00:29:06

标签: c# linq functional-programming

Say I have an arbitrary list [2, 5, 6, 2, 2, 4]

Now given a list, let call it list A [1,2,3,4,5,6,0].List A contain all the number in the arbitrary list in a certain order. Now let arrange the first list based on the order of List A. Each new order will become a new list. So the outcome should be

[2,5,6]
[2]
[2,4]

Another example if the list is splitted based on the order of list B [4,5,6,0,1,2,3] then the outcome should be

[2]
[5,6,2]
[2]  
[4] <-- the different is that now the (ordered?) list change, the 4 is now belong on the next row.

I want to do this in LINQ or a functional way. I have an iterative solution that I posted in my original question please don't read it until you have answered or attempted to answer this question as I don't want to introduce iterative bias thinking into the answer...

Split a list or ordered dates into weeks using linq

Or see below for the iterative answer

var orders = new List<int> { 4, 5, 6, 0, 1, 2, 3 };
var nums = new List<int> {2, 5, 6, 2, 2, 4};


        var queue = new Queue<int>(nums);
        var results = new List<List<int>>();
        while (queue.Count > 0)
        {
            var subLists = new List<int>();
            foreach (var order in orders)
            {
                if(order == queue.Peek())
                    subLists.Add(queue.Dequeue());


                if (queue.Count == 0)
                    break;
            }

            results.Add(subLists);
        }

2 个答案:

答案 0 :(得分:1)

我会通过遍历您希望一次拆分一个元素的列表来完成此操作。

对于每个元素,在列表中找到定义拆分顺序的索引。将其与前一项的索引进行比较,如果它更大,则将其添加到临时列表中。否则,将临时列表添加到结果列表,并将临时列表设置为仅包含此项目的新列表。

继续以这种方式循环,直到获得<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <span> <a href="#">link</a> </span> </div> <div> <span> <span title="title 1">Some text written here</span> </span> </div> <div> <span> <span title="title 2">Some other text written here</span> </span> </div> <div> <span> <a href="#">some link</a> </span> </div>结果。

代码可能更容易理解:

List<List<int>>

答案 1 :(得分:0)

使用for循环非常简单:

            List<int> orderList = new List<int>(){ 1, 2, 3, 4, 5, 6, 0 };

            List<int> input = new List<int>(){ 2, 5, 6, 2, 2, 4 };

            List<List<int>> output = new List<List<int>>();
            List<int> temp = null;

            for (int i = 0; i < input.Count(); i++)
            {
                if (i == 0)
                {
                    temp = new List<int>();
                    output.Add(temp);
                    temp.Add(input[i]);
                }
                else
                {
                    if (orderList.IndexOf(input[i]) > orderList.IndexOf(input[i - 1]))
                    {
                        temp.Add(input[i]);
                    }
                    else
                    {
                        temp = new List<int>();
                        output.Add(temp);
                        temp.Add(input[i]);
                    }
                }
            }
相关问题