Sum concecutive strings

Hello Everyone ,

Could someone help me in acheiving required list arrangement

Welcome to the forums!

Can you show us what you’ve tried so far? This is probably trickier than it seems, but there are a few ways you could attempt to go about it. Python would certainly be the easiest but you could probably do it with nodes as well.

1 Like

Edit: Removed, misinterpreted your request given the simplified example.

Edit 2: Actually, that solution does what I think you’re after with a couple changes:

That custom node is just this Python:

from itertools import groupby

group = groupby(IN[0])

OUT = [list(i) for j,i in group]

Welcome to the forums. This is not a ‘do my work’ forum, so you’ll usually get better responses if you show what you’ve attempted so far so that other users can guide you on ways to improve your script.

In this case, I think it’ll be of immediate benefit to start thinking of terms and phrases to use in the search tool.

This thread should be what you are looking to do, I came across it by looking up “Grouping”, “Consecutive”, “Repeated”, and “Sequential” until something that matched the problem was found.


Same script as the link, but with nodes grouped and explained

Two more approaches for you.

Python approach:

from itertools import groupby
lst = IN[0]
OUT = [[key + '-' + str(len(list(group))) for key,group in groupby(sublst)]for sublst in lst]

DesignScript approach:

sublsts = DSCore .List.Sublists(x<1>,0..1,1);
first_itm = DSCore.List.FirstItem(sublsts<1><1>);
pairs = sublsts == first_itm;
bools = DSCore.List.AllTrue(pairs<1><1>)? false: DSCore.List.AllFalse(
		pairs<1><1>)? false:true;
seq = DSCore.List.AllIndicesOf(bools<1>,true)+1;
add_front = 0..0..#DSCore.List.Count(seq);
add_end = DSCore.List.Count(bools<1>);
positions = DSCore.List.AddItemToFront(add_front<1>,
            DSCore.List.AddItemToEnd(add_end<1>,seq<1>)<1>);
lengths = DSCore.List.RestOfItems(positions<1>)
            -DSCore.List.DropItems(positions<1>,-1);
groups = DSCore.List.Chop(x<1>,lengths<1>);
count = DSCore .List.Count(groups<1><1>);
out = DSCore.List.FirstItem(groups<1><1>) + "-" + count;
3 Likes

Another solution

import sys

lstInput = IN[0]

OUT = [sorted(["{}-{}".format(txt, sublst.count(txt)) for txt in set(sublst)]) for sublst in lstInput]
3 Likes

Thankyou Everyone .
I will go through all the solutions provide by eachone of you