Grasshopper

algorithmic modeling for Rhino

I am relatively new to scripting and have struggled implementing a script that will return all combinations of length k from set(list) m.  

Cross reference is not feasible as multiple values of k will be fed in and they will get well into double digits.

There are a multitude of codes online in python and vb and c# to this end, but I have been unable to make any of them work within grasshopper.

If anyone has worked through this problem already or has some insight please let me know.

Jackson

Views: 1224

Replies to This Discussion

http://docs.python.org/2/library/itertools.html#itertools.combinations

I'm not sure if IronPython has itertools, but in standard python, there is an easy command for this:

import itertools

for combo in itertools.combinations(m, k):

    # do something with the combo

itertools appears to be implemented in IronPython 2.7:

http://ironpython-test.readthedocs.org/en/latest/library/itertools....

Benjamin,

I had seen itertools, but could not figure out how to get it to print or output the combinations.

I've attached an example file where the set is the list of numbers from 0 to 19 for use as indexes later and the k value is just 5.  The goal is to output all combinations of length 5 from the list of integers 0 to 19.

I placed the list in brackets in order to make it iterable (I don't really know what this means.)  Somehow I need to get the combinations out of the itertools.combination output.

Attachments:

At this point I have figured out the itertools so I would posit a second question, how to unpack the tuples of combinations, so that I can have a branch for each combination (each list of indices to pull from the original set).  So that ultimately each combination is in its own branch.

I cannot solve this in python but was through string manipulation, able to do so with grasshopper components. I will post the file here.

Jackson

Attachments:

In case it is helpful, here's an example of making branches on a data tree with Python:

https://gist.github.com/bengolder/1974640

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service