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: 7970

Replies to This Discussion

Hey,

if you have every time this 2 lists -one even, one odd-you can use the "Sort" component to put them together.

I dont work with python but i can give you the C# equivalent:

private void RunScript(List<object> x, List<object> y, ref object A)
{

List<int> my_list = new List<int>();

foreach (int item_even in x)
{
my_list.Add(item_even);
}

foreach (int item_odd in y)
{
my_list.Add(item_odd);
}

my_list.Sort();

A = my_list;

}

as you can see its the same procedure. Put the two lists together, sort and output.

Hope this helps,

R.

By omitting the 2 obvious ways , here's 5 "other" ways to skin the cat (but it's C# not Python).

But which is best?

he he

Attachments:

nice one...

hey guys!
I'm really glad you answered so quickly, but none of these helps with learning PYTHON...
I know how to figure out this task in grasshopper but what I am trying here is to translate examples i could do in grasshopper to Python, so that i can learn how to script properly...

I would really appreciate some hints exclusively for PYTHON.

but still, thanks a lot

well, you are in the wrong forum ;-). You should go in "VB, C# and Python" with your question.

Well... Python is like my KTM RC8R (fast and furious), C# is like my FireBlade (the benchmark, get one now).

Moral: ride the Blade, what else?

he he

list1 = [0,2,4,6]
list2 = [1,3,5]
mergedList = list1+list2
mergedList.sort()
print mergedList   #[0, 1, 2, 3, 4, 5, 6]

Hello maxK,

I am nor sure whether my solution is good for you to learn. But is only 3 lines of code. See the attached.

best,

M.

EDIT: Now that I think about it, ignore my solution. It complicates things. Djordje's one is more appropriate for your progress

Any reason for using python on this task?

There's a native component for exactly that task.

well the thing is that i actually have 2 lists with points, not numbers. what i want to do is make up a list to generate a polyline from. (polyllinepointslist)

the list (polylinepointslist) should contain pointlist1[0],pointlist2[0],pointlist1[1],pointlist2[1],pointlist1[2],pointlist2[2]....

i know all the components in grasshopper to do exactly this kind of task, but i want to try to manage this task in written PYTHON CODE!
I'm new to python but i think that it could help me learn and understand that stuff, to actually translate grasshopper "bricks" to written PYTHON code...

You problem is a pretty basic Python problem:

"Iterate trough two lists in parallel. Add the items of both list sequentially to the list of results."

In Python such a combination of two lists is done with the zip or map function, depending on the desired combination behavior. Look at the linked example and add both variables sequentially to a result list inside the loop instead of just printing them.

As a side note: the way Python solves a specific problem may or may not differ hugely from the way you would solve this in any other language like C# or VB or even Grasshopper.

Hey Hannes!
Thanks a lot!
I can imagine that Python requires a different way of thinking/solving problems. And I already see that some tasks are way easier to solve in GH than scripting them in Python but still...It's all just about getting familiar with the python synthax...and since I don't know how to learn it besides trying to solve specific tasks I will try to continue working on tasks that i already managed to produce with GH and translate them into Python scripts...
So please excuse all my basic questions still to come ;)


RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service