Comment appliquer une fonction sur une liste possédante une multi-imbrication ?
if this article is not in your language, use the Google Translate widget (bottom of page for Mobile version)⬈
Il peut arriver que l'on souhaite restituer au travers d'un nœud Python la même structure que la liste d'entrée.
Voici un exemple avec une fonction récursive appelant une autre fonction (opération à effectuer).
Ici on convertit une liste de points en liste sphère en gardant la même structure.
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
def convertPointToSphere(pt):
return Sphere.ByCenterPointRadius(pt, 1)
def nestedRecursFunc(lst, functoApply):
newlst = list(lst)[:] # copy input list and convert it (if necessary)
for idx, elem in enumerate(lst):
if isinstance(elem, list) and len(elem) > 0:
newlst[idx] = nestedRecursFunc(lst[idx], functoApply)
elif isinstance(elem, list) and len(elem) == 0:
newlst[idx] = []
else:
newlst[idx] = functoApply(lst[idx])
return newlst
multi_NestedList = IN[0]
OUT = nestedRecursFunc(multi_NestedList, convertPointToSphere)
0 commentaires:
Enregistrer un commentaire