Domain from a value pattern

Hello,

How would one go on about counting consecutive items in a list?

I would like to convert a boolean pattern to a domain:

To create sublists for repeating patterns.

I guess it’s a fairly simple loop, I just can’t figure it out…

The logic I have followed is to shift the list, and compare each element to the one next to it. This gives a list of indexes where a “change” happens, which I then use to split the original list.

The problems happen at the beginning and end of the list, where there is no “adjacent” value to compare to. This is where the fudging happens, by inserting the indexes of the first and last members.

DivideByConsecutive.gh (12.2 KB)

I’m sure there’s a more elegant way to do this :man_shrugging:


split_list_ in_sublists.gh (10.3 KB)

A more pythonic approach to that is using the itertools.groupby function :slight_smile:

from itertools import groupby
c = [list(g) for k, g in groupby(x)]

split_list_ in_sublists.gh (9.2 KB)

3 Likes

wow that looks efficient indeed…