Change list structure to match another list structure

Might be late to the party but thought it would be a fun test of recursive functions in python:

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

def recCount(_lists,c):
	for _list in _lists:
		if isinstance(_list,list):
			c = recCount(_list,c)
		else:
			c += 1
	return c

def recMatch(_list,_matches,_out,c):
	for _match in _matches:
		if isinstance(_match,list):
			outTemp, c = recMatch(_list,_match,[],c)
			_out.append(outTemp)
		else:
			_out.append(_list[c])
			c += 1
	return _out, c
		

# The inputs to this node will be stored as a list in the IN variables.
mList = IN[0]
toMatch = IN[1]
outList = []
c = 0
mC = 0
if len(mList) == recCount(toMatch,c):
	OUT = recMatch(mList,toMatch,outList,mC)[0]
else:
	OUT = ["List lengths do not match.","IN[0] length = "+ str(len(mList)), "IN[1] length = " + str(recCount(toMatch,c))]

Things to note:
mList (IN[0]), which is the list you want to change format of, must be completely flatten. The script will test for flatten and output the error message. It will also check for equal lengths of both lists, regardless of the levels.

I have not tested it out for empty lists though. That is my next step.

9 Likes