Grasshopper

algorithmic modeling for Rhino

hey guys!
i want to merge two lits alternating...

list1 = [0,2,4,6]

list2 = [1,3,5]

??how would the merging-script (python) look like??

(deisred):    merge_list =[0,1,2,3,4,5,6]

thank you guys!

Views: 7973

Replies to This Discussion

I took a stab at this.  I am not sure it's the best way, but it seems to work.  I dumped the following code into a GH Python component.  Requires equal length input lists, (probably smarter to add some type of list length checking or something).

Outputs two lists.

List x = corresponding items from each list merged into one list item (actually outputting a list of tuples...more info here: https://docs.python.org/2/library/functions.html?highlight=zip#zip

List y = alternating items from each list combined into one list. (more info here: https://docs.python.org/2/library/itertools.html#itertools.chain

I never used these python methods, I just learned about them.  I have absolutely no idea how efficient this is and what it would do with larger lists.  But, it seems to be a basic python method for "weaving" two lists?

Need to change the inputs a and b to "List access"

import rhinoscriptsyntax as rs
import itertools

List1 = zip(a, b)
List2 = itertools.chain.from_iterable(List1)

x = list(List1)
y = list(List2)

Nice solutions Thank you  Chris

All you need is Weave.

It puts together two lists in alternating fashion or according to a pattern, similar to an inverse of the Dispatch component.

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