I’m new on Dynamo Phyton Scripting.So any help will be appreciated.
I have a python script that creates pairs of all possible combinations from a list. I would like to implement it into Dynamo. Not sure how???
You can use that exact same code inside a python node, and only change how you take in the input values
Have you tried it?
If you haven’t done any dynamo/python at all, take a look at Dynamo Primer’s intro to Python Node:
http://dynamoprimer.com/en/09_Custom-Nodes/9-4_Python.html
-
Please paste your code as text, instead of an image, if you’d like it to be reviewed. (Use the preformatted text option )
-
You could try Dynamo’s built-in “Combinations” node as an alternative:
ok.
I did try the List.Combinations node but I did not get what I needed.
Thanks for the help.
import clr clr.AddReference('ProtoGeometry') from Autodesk.DesignScript.Geometry import * #The inputs to this node will be stored as a list in the IN variables. dataEnteringNode = IN stringlist = IN[0] import itertools def generate_groups(lst, n): if not lst: yield [] else: for group in (((lst[0],) + xs) for xs in itertools.combinations(lst[1:], n-1)): for groups in generate_groups([x for x in lst if x not in group], n): yield [group] + groups stringlist.append(list(generate_groups([stringlist], 2))) #Assign your output to the OUT variable. OUT = stringlist
The problem arises from the fact that you’re wrapping your input list in another list when you’re calling the function:
generate_groups(**[**stringlist], 2)))
This should do it:
stringlist = IN[0]
import itertools
def generate_groups(lst, n):
if not lst:
yield []
else:
for group in (((lst[0],) + xs) for xs in itertools.combinations(lst[1:], n-1)):
for groups in generate_groups([x for x in lst if x not in group], n):
yield [group] + groups
OUT = generate_groups(stringlist, 2)
Many thanks Dimitar_Venkov and salvatoredragotta… It makes more sense now.