splitting list of numbers on each sequence of numbers

r split column by number of characters
r split number into digits
r split column into multiple columns by separator
range extraction golang
r split string into multiple columns
r split column by comma
separate function in r
list of numbers in python

I have been working on this problem for a couple days now and I can not seem to get the exact result I am looking for. I have simplified my question to just number based so that I can be as clear as possible on what exactly I am doing and what I want in return.

I start off with a big List<List<double>> where each sub List in the larger List contains 3 numbers. For example, the List looks something like this:

[0] 1,2,3

[1] 1,2,3

[2] 1,2,3

[3] 4,5,6

[4] 4,5,6

[5] 4,5,6

[6] 7,8,9

[7] 7,8,9

[8] 7,8,9

where each item in the list is a different sequence. What I am trying to accomplish is to separate the List into a group of smaller lists where each item in the list are all similar. So for the given example:

list1:

[0] 1,2,3

[1] 1,2,3

[2] 1,2,3

list 2:

[0] 4,5,6

[1] 4,5,6

[2] 4,5,6

list 3:

[0] 7,8,9

[1] 7,8,9

[2] 7,8,9

So, for solve my problem I have created a function to recursively search through the List and pull out the sequences that are similar and add them to separate lists. Not only is my function not working, but my code is very long and complicated and I feel like there should be a similar solution than what I am trying to do. Any suggestions or advice to get me going in the right direction will be appreciated.


I think this will do it for you. It works with your 'out of order' requirement -- that is, {1,2,3} equals {3,2,1} equals {2,3,1}.

static void Main(string[] args)
{
    List<List<double>> list = new List<List<double>>()
    {
        new List<double>() { 1,2,3 },
        new List<double>() { 4,5,6 },
        new List<double>() { 7,8,9 },

        new List<double>() { 2,3,1 },
        new List<double>() { 5,6,4 },
        new List<double>() { 8,9,7 },

        new List<double>() { 3,1,2 },
        new List<double>() { 6,4,5 },
        new List<double>() { 9,7,8 },
    };

    // Pick a method, they both work
    //var q2 = DictionaryMethod(list);
    var q2 = LinqAggregateMethod(list);

    foreach (var item in q2)
    {
        Console.WriteLine("List:");
        foreach (var item2 in item)
            Console.WriteLine($"\t{item2[0]}, {item2[1]}, {item2[2]}");
    }
}

static bool ListsAreEqual(List<double> x, List<double> y)
{
    foreach (var d in x.Distinct())
    {
        if (x.Count(i => i == d) != y.Count(i => i == d))
            return false;
    }
    return true;
}

static IEnumerable<IEnumerable<List<double>>> LinqAggregateMethod(List<List<double>> list)
{
    var q = list.Aggregate(new List<List<double>>() /* accumulator(ret) initial value */, (ret, dlist) =>
    {
        // ret = accumulator
        // dlist = one of the List<double> from list

        // If accumulator doesn't already contain dlist (or it's equal), add it
        if (!ret.Any(dlistRet => ListsAreEqual(dlist, dlistRet)))
            ret.Add(dlist);
        return ret;
    });
    // At this point, q contains one 'version' of each list.

    // foreach item in q, select all the items in list where the lists are equal
    var q2 = q.Select(dlist => list.Where(item => ListsAreEqual(dlist, item)));
    return q2;
}

static IEnumerable<IEnumerable<List<double>>> DictionaryMethod(List<List<double>> list)
{
    var list2 = new Dictionary<List<double>, List<List<double>>>();
    // Loop over each List<double> in list
    foreach (var item in list)
    {
        // Does the dictionary have a key that is equal to this item?
        var key = list2.Keys.FirstOrDefault(k => ListsAreEqual(k, item));
        if (key == null)
        {
            // No key found, add it
            list2[item] = new List<List<double>>();
        }
        else
        {
            // Key was found, add item to its value
            list2[key].Add(item);
        }
    }
    var q2 = new List<List<List<double>>>();
    foreach (var key in list2.Keys)
    {
        var a = new List<List<double>>();
        a.Add(key); // Add the key
        a.AddRange(list2[key]); // Add the other lists
        q2.Add(a);
    }
    return q2;
}

Python splitting list based on missing numbers in a sequence, From the python documentation: >>> # Find runs of consecutive numbers using groupby. The key to the solution >>> # is differencing with a range so that  Quick Tip: Split a sequence of numbers or letters into multiple columns. Separating a text value into multiple columns by using a separating character is easy. You can use ‘separate’ command from ‘tidyr’ package like below. I’m separating ‘location’ data into ‘City’ and ‘State’ column separating by ‘,’ (comma).


Here is another approach to this problem. I would divide it into 2 Steps.

// Sample input:
List<List<double>> lists = new List<List<double>>();
lists.Add(new List<double> { 1, 1, 3 });
lists.Add(new List<double> { 1, 3, 1 });
lists.Add(new List<double> { 3, 1, 1 });
lists.Add(new List<double> { 4, 5, 6 });
lists.Add(new List<double> { 4, 5, 6 });
lists.Add(new List<double> { 6, 5, 4 });
lists.Add(new List<double> { 7, 8, 9 });
lists.Add(new List<double> { 8, 7, 9 });
lists.Add(new List<double> { 9, 8, 7 });

1) Get all unique lists from your collection. You can order them temporarily with OrderBy. This will allow a comparison using SequenceEqual:

List<List<double>> uniqueOrdered = new List<List<double>>();

foreach (var element in lists.Select(x => x.OrderBy(y => y).ToList()))
{
    if (!uniqueOrdered.Any(x=> x.SequenceEqual(element)))
    {
        uniqueOrdered.Add(element);
    }
}

2) Now you have a set of representatives for each of your groups. Run through each representatives and get all lists that match the elements in your representative. Again here you can order them temporarily for the sake of comparison with SequenceEqual:

List<List<List<double>>> result = new List<List<List<double>>>();

foreach (var element in uniqueOrdered)
{
    result.Add(lists.FindAll(x=> x.OrderBy(t=>t).SequenceEqual(element)));
}

The lists in the resulting groups will maintain their original order!

How do I split a list of numbers to monotonic sequences , Starting at 10.1, there's a fairly neat solution using SequenceCases : list = {1, 2, 4, 5, 7, 11, 8, 7, 3, 1, -3, -2, 6, 7, 80}; SequenceCases[list, x_ /; Less @@ x  2) Column function is used to return the column number of a reference. Let’s take an example to understand how we can separate the number into digits. Column A contains a list of numbers. Separating the number in A2 into digits, so that each of cells B2:G2 contains one digit. Follow below given steps:-Write the formula in cell B2.


I tried my best to shorten the needed code to accomplish what I assume you want to accomplish. By the way I put the resulting lists in a list you'll see:

this following sample is just here to declare your list and put random values:

  List<List<int>> ContainerList = new List<List<int>>()
        {
            new List<int>()
            {
                0, 1, 2
            },
            new List<int>()
            {
                3, 4, 6
            },
            new List<int>()
            {
                0, 1, 2
            },
            new List<int>()
            {
                7, 8, 9
            },
        };

Now begins the payload:

        List<List<List<int>>> result = new List<List<List<int>>>();

        foreach (var cont in ContainerList)
            result.Add(ContainerList.FindAll(x => x.SequenceEqual(cont)));

        // the following erase duplicates
        result = result.Distinct().ToList();

So Now you can get your sublists as:

[0][0] 012

[0][1] 012

[1][0] 346 ....

EXPLANATION:

 ContainerList.FindAll(x => x.SequenceEqual(cont))

The Following snippet use a predicate: the x here is a value in your list.

As it is a list of list, your x gonna be a list

SequenceEqual means that the Findall function will search equalities by VALUE and not by REFERENCE.

Next we erase duplicates because the Findall on the first element of the ContainerList will return a list containing all his duplicates correspounding to the given parameter (which is x).

But as the parameter (x) increments in the list. You gonna do as many FindAll as there are Values of the same subset. So in the example above, you gonna have 2 lists of 2 012;

I hope it is understandable. My english is terrible.

Quick Tip: Split a sequence of numbers or letters into multiple columns, Use 'list_to_text' function from 'exploratory' package to convert the list to text. This is when we can add a separating character between the each letter. The const input string has 4 numbers in it: they are one or two digits long. To acquire the numbers, we use the format string @"\D+" in the Regex.Split method. Pattern: The pattern "\D+" indicates that we want to use any number of one or more non-digit characters as a delimiter.


This is the code that I would use for the example that you gave.

    static void Main(string[] args)
{
    List<List<double>> lists = new List<List<double>>();
    lists.Add(new List<double> { 1, 2, 3 });
    lists.Add(new List<double> { 1, 2, 3 });
    lists.Add(new List<double> { 1, 2, 3 });
    lists.Add(new List<double> { 4, 5, 6 });
    lists.Add(new List<double> { 4, 5, 6 });
    lists.Add(new List<double> { 4, 5, 6 });
    lists.Add(new List<double> { 7, 8, 9 });
    lists.Add(new List<double> { 7, 8, 9 });
    lists.Add(new List<double> { 7, 8, 9 });
    lists.Add(new List<double> { 7, 8, 9 });

    List<List<List<double>>> sortedLists = new List<List<List<double>>>();

    for (int i = 0; i < lists.Count; i++)
    {
        bool found = false;
        if (!(sortedLists.Count == 0))
        {
            for (int j = 0; j < sortedLists.Count; j++)
            {
                if (lists[i][0] == sortedLists[j][0][0] && lists[i][1] == sortedLists[j][0][1] && lists[i][2] == sortedLists[j][0][2])
                {
                    found = true;
                    sortedLists[j].Add(lists[i]);
                    break;
                }
            }
        }
        if (!found)
        {
            sortedLists.Add(new List<List<double>> { lists[i] });
        }
    }
}

The only thing is that the inner if statement is designed for this example specifically.

if (lists[i][0] == sortedLists[j][0][0] && lists[i][1] == sortedLists[j][0][1] && lists[i][2] == sortedLists[j][0][2])

This would have to be changed if you used anything out side of 3 double lists.

Range extraction, Create a function that takes a list of integers in increasing order and returns a type Sequence is array (Positive range <>) of Integer; static IEnumerable<(int start, int end)> Ranges(IEnumerable<int> numbers) { The monotonic-split word enables us to split the input sequence into sub-sequences of contiguous integers​. Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers. Sample data: 3, 5, 7, 23. Python list: A list is a container which holds comma separated values (items or elements) between square brackets where items or elements need not all have the same type. In general, we can define a list as an object that contains multiple data items (elements).


Python, Python | Reverse sequence of strictly increasing integers in a list · Python - Test for strictly decreasing list · Python | Split list in uneven groups · Python program to​  1. Select the number cells, and click Kutools > Text > Split Cells. See screenshot: 2. In the Split Cells dialog, check Split to Columns option in Type section, and then go to check Specify width option in Split by section, and enter 1 into the beside textbox. See screenshot: 3.


Split the number into N parts such that difference between the , Print the sequence in the end, if the number cannot be divided into exactly 'N' parts then Approach: There is always a way of splitting the number if X >= N . As you can see, this will yield digits from right to left. If you'd like the digits from left to right, you'll need to create a sequence out of it, then reverse it: You can also use this function for base conversion as you split the integer.


Lists - Learn Python 3, A list is a sequence of elements numbered from 0, just as characters in the string. You can obtain the number of elements in a list with the function len (meaning length You can then use a string method split() , which returns a list of strings  int[] intTab = String.valueOf(number).chars().map(Character::getNumericValue).toArray(); To be clear: You use String.valueOf(number) to convert int to String, then chars() method to get an IntStream (each char from your string is now an Ascii number), then you need to run map() method to get a numeric