Partitioning lists based on a jump in consecutive values

Hi there,

I have a problem i have been working on and cant seem to get it.

I have a data tree with different lists in it. Now I want to partition this data based on whether a value jumps from consecutive numbers to a higher number. (instead of N+1, it jumps to N+…).

example: data = 1,2,3,4,5,6,22,23,24,25,26,27.
Now i want to partition this list into: 1,2,3,4,5,6 AND 22,23,24,25,26,27.
For one list this can be done manually, but i have many of these lists in my data tree of which the jump in value of the consecutive numbers is not constant. How do i partition this parametrically?

many thanks in advanced!

1 Like


PartitionNumbers_re.gh (11.7 KB)

2 Likes

Thanks alot Kim!

You have an elegant way of doing this. In the meantime i got my own work around seen in the image here, which seems to work fine. but ill see if your method has benefits for my script;)

Kim!

i see now that my node setup doesnt work at all XD thanks alot, i will still use your way of doing this!

greetings!

based on @HS_Kim start:


File:PartitionNumbers_bw.gh (8.8 KB)

3 Likes

HS_Kim - how do you display the name of the component above it? I like that look, but can’t seem to find the setting! Thanks! Tom

It’s the sunglasses plug in:

2 Likes

Thanks!

this does not work for me since i use a data tree with several lists… de “replace list” component does not work since the output of the mass mulitply and the output of the “path as list” do not match in amount of items. im trying to match them in some sort of way, but cant seem to figure it out :frowning: anyone with a quick solution?

This works nice, but what if the initial list has jumps with different values in between. For example:
1, 2, 3, 4, 5, 6, 18, 20, 22, 24, 26, 35, 40, 45, 50, 66, 67, 68, 69, 70.

So it should be split into (1, 2, 3, 4, 5, 6), (18, 20, 22, 24, 26), (35, 40, 45, 50) and (66, 67, 68, 69, 70).

So the difference in the first list is 1, then 2, then 5 and then 1 again.

Can you guarantee that the data has no ambiguities?

For example, should 1,2,3,4,5,10,15,20 be grouped (1,2,3,4,5) (10,15,20) or (1,2,3,4) (5,10,15,20)?

If you can guarantee that the smallest gap between groups is bigger than the largest jump between values in a group, there’s this fantastic hack I saw from @Adam_M using Point Groups:

1 Like

That is a very nice way to go about it, but what if I cannot guarantee that? In the case of ambiguities it doesn’t matter if the ambiguous number goes into the first or the second group.

Please consider the following screenshot as proof that this problem is probably best solved with some Python.

The algorithm would be something like:

  1. Find all sublists by detecting a change in delta and ID them sequentially. This will include lists of length two which indicate a jump between genuine sublists, or between unrelated digits.
  2. Cull all lists of length 2
  3. If a sublist’s ID is exactly +1 from the preceding one, cull its first element

I must leave it to a coder to code.

1 Like

Inspiration came to me on my bike ride :slight_smile:

This draws a graph of each list of numbers, then uses Polyline Reduce to find the domains of the sequences, including those with only two elements. Then we Sub List with those domains, prune out the 2-element lists, and delete the last item of all but the last overlapping lists in a sequence of overlapping lists (which is done via a happy accident of Relative Item not returning results when the Offset cannot find a target). Then do a little tree juggling to reassemble the final result as 0;source#;0;0;sequence#.

I have tested it with short lists with what I think are all the edge cases (multiple overlapping lists, single numbers with no sequence, lists with the same delta next to each other etc). Please let me know how it copes with real data.

I’m sure there’s a way to get the domains without drawing a graph, but it’s so cute I didn’t bother trying :smiley:

Find Sequences by Graph.gh (17.5 KB)

3 Likes