Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "split list" method in c#?

In C#, calling the .Split method will split a string into an array of strings based on some character or string.

Is there an equivalent method for lists or arrays?

For example:

var foo = new List<int>() { 1, 2, 3, 0, 4, 5, 0, 6 };
var output = Split(foo, 0);
// produces { { 1, 2, 3 }, { 4, 5 }, { 6 } }

This is what I have so far -- is there a cleaner or more eloquent way of accomplishing the same task?

IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> list, T divider)
{
    var output = new List<List<T>>();
    var temp = new List<T>();
    foreach ( var item in list )
    {
        if (item.Equals(divider))
        {
            output.Add(temp);
            temp = new List<T>();
        }
        else
        {
            temp.Add(item);
        }
    }

    output.Add(temp);
    return output;
}

Edit:

It just occurred to me that my version will split the list only by a single element, whereas string.Split can split using either a single character, or an arbitrary string.

Just for the sake of completeness, what would be the best way to implement that?

like image 935
Michael0x2a Avatar asked Jul 18 '13 22:07

Michael0x2a


People also ask

How do you separate list items?

Usually, we use a comma to separate three items or more in a list. However, if one or more of these items contain commas, then you should use a semicolon, instead of a comma, to separate the items and avoid potential confusion.

How do I split a linked list into two?

1) Store the mid and last pointers of the circular linked list using tortoise and hare algorithm. 2) Make the second half circular. 3) Make the first half circular. 4) Set head (or start) pointers of the two linked lists.

Is there any list in C?

There is no such thing as a list in C.


2 Answers

No built-in equivalent, but a lazy-evaluated one would be

IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> list, T divider)
{
    var temp = new List<T>();
    foreach (var item in list)
    {
        if (!item.Equals(divider))
        {
            temp.Add(item);
        }
        else
        {
            yield return temp;
            temp = new List<T>();
        }
    }

    if(temp.Count>0) yield return temp;
}
like image 78
I4V Avatar answered Sep 22 '22 01:09

I4V


No, there is no special existing method in the framework to split sequence.

You code is reasonable.

Routes to improve/change:

  • You may be able to use yield return instead of adding to output to gain some lazy evaluation.
  • With even more interesting code you can make inner lists lazy too (which may be important if incoming sequence is not bound/too long segments).
  • And you can using Aggregate if you want to show off single statement code...
like image 41
Alexei Levenkov Avatar answered Sep 23 '22 01:09

Alexei Levenkov