Replace consecutive items in list

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

lstMaster = IN[0]
indiceLst = []

for lst in lstMaster:
	indices = [i for i, x in enumerate(lst) if x == "Landing"]
	indiceLst.append(indices)

targetInfo = []

boolResult = []
for lst in indiceLst:
	
	info = ""
	itemToBeReplaced = ""
	
	if len(lst) == 1:
		boolResult.append(False)
		continue
	sum = 0
	delta = lst[0]
	for i, num in enumerate(lst):
		if num - i == delta:
			sum += 1
	if sum == len(lst): 
		boolResult.append(True)
		for i, num in enumerate(lst):
			if not (i % 2 == 0):
				itemToBeReplaced = str(num) + "\n"
		info = "Bizarre List " + str(indiceLst.index(lst)) + "\n" + "Item to be replaced at index " + itemToBeReplaced 
		targetInfo.append(info)		
	else: boolResult.append(False)
	
# Assign your output to the OUT variable.
OUT = indiceLst, boolResult, targetInfo

Had to copy/paste it into Visual Studio Code and copy from there, then the formatting worked :slight_smile:

4 Likes

@Paul_Wintour An Associative Design Script approach. No loops


replAltRep.dyn (7.6 KB) (ver 2.1)

a = "";
b = "landing";
c = [[a,a,b,a],[a,b,b,b,a,b],[b,b,b,b,b]];

d = List.Sublists(c<1>,(0..1),1);
e = List.Contains(d<1><2><3>,b);
f = List.AllTrue(e<1><2>);
g = List.AllIndicesOf(f<1>,true);
i = List.TakeEveryNthItem(g<1>,2,0);
j = List.SetDifference(0..List.Count(c<1>)-1,i);
k = List.Cycle(a,List.Count(i<1>));
l = List.GetItemAtIndex(c<1>,j<1>);
m = List.Flatten(List.Transpose([l,k])<1>,-1);
n = List.Flatten(List.Transpose([j,i])<1>,-1);
o = List.SortByKey(m<1>,n<1>)["sorted list"];

The same with nodes…


replAltRep-nodes.dyn (41.4 KB) (ver 2.1)

3 Likes

Lots of solutions. Spoilt for choice. Thanks for your help everyone.