Grasshopper

algorithmic modeling for Rhino

[Python] Redirecting output of a "for" loop into two-dimensional lists

Hi guys,

i'm quite new at Python scripting, asking your help.

I've got a loop in a loop (two consecutive "for" loop).

Is there a way to create a two dimensional list (whose number of item (list) is the count of the first loop variable).

I'm thinking to something like

list = [list1,list2,list3,...,listN]  #where N is the number of cicle in the first loop

list1   is the result of cicle in the second loop (caused by the second variable), like list 2m list3, and so on till to listN..

I've tried like this, but it doesn't work:

MainList = []
SubList = []

for i in range(len(InputList_1)):
MainList.append(SubList)
for j in range(len(InputList_2)):
MainList[i].append(rs.TransformObject(InputList_1[i],XformMatrix[j]))

it retrieves this error:

Runtime error (ArgumentTypeException): __getitem__() takes exactly 2 arguments (1 given)

Views: 803

Replies to This Discussion

The concept is to create in grasshopper one branch each sublist, but avoiding gh data structure, and creating it in python.

Do you mean making a nested list using two for for loops? If so you can try something like this..

It's almost like that.

I've updated the code, and it works now, but not at all.
It creates too much lists.

This is the code

MainList = []
SubList = []
for i in range(len(InputList_1)):
    for j in range(len(InputList_2)):
        SubList.append(rs.TransformObject(InputList_1[i],MirrorMatrix_L[j],True))
        MainList.append(SubList)

I've just culled the  [i] from the last .append

The real problem is that it copies the both the two list j times. Here is a screenshot

2 Different Lists (in different branch) 6 times, for totally 12 branches.

Thanks for the reply.
I tried both ways and it doesn't work...

Something else?

It has been quite easy to extract the right values in a flat python list.
And i've found a way to post-create sublists as temporary way, but still looking for a cleaner way =)

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service