List Create (With Strings)

Can your python code [1] also work with strings? @c.poupin

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

lst = IN[0]

def splitBySequence(lst):
	if lst:
		grouplst = [lst.pop(0)]
		for i in lst:
			if i -1 == grouplst[-1]:
				grouplst.append(i)
		lst = [i for i in lst if i not in grouplst]
		return [grouplst] + splitBySequence(lst)
	else:
		return []
	
OUT = splitBySequence(lst)

strings test.dyn (14.8 KB)

[Ref 1]

That’s just not how strings work. Try converting the string to a number to compare and then convert back.

2 Likes

Works perfectly with what @Nick_Boyts suggested.

lst = map(lambda x: ord(x), IN[0])

def splitBySequence(lst):
	if lst:
		grouplst = [lst.pop(0)]
		for i in lst:
			if i -1 == grouplst[-1]:
				grouplst.append(i)
		lst = [i for i in lst if i not in grouplst]
		return [grouplst] + splitBySequence(lst)
	else:
		return []
	
OUT = map(lambda y: [chr(x) for x in y], splitBySequence(lst))
1 Like