Grasshopper

algorithmic modeling for Rhino

I'm making my own GHPython script.

I want to extract the list number where True is stored, with the input value X as the list that returns True or False, but I do not know the intrinsic function to use.

Please tell me how to write it.

Views: 1962

Attachments:

Replies to This Discussion

You can simply use the Member Index component.

If you still want to write GHPython script then maybe you can loop through the list with enumerate() in order to retrieve all the indexes of True values, then append them to a new list. See attached.

Attachments:

Hi vhoang

I am surprised that there is a simple GH component.
Your code in python is also helpful.

Thank you!!

--

Tomo

as mentioned, enumerate is probably what you are looking for. From a strictly pythonic approach, it would be some variation of this:

index = [] #list to hold the index, named component output

value = [] #list to hold the value, named component output

enumeratedList = list(enumerate(x)) #example to show results of enumerate

for i,j in enumerate(x): #i is first value, j, is second value

    if j == y: #set input hint type to string and connect boolean toggle

        index.append(i) #append i, (first value) to index list

        value.append(j) #append j, (second value) to value list

Thank you!

I thought using the "index" method provided by the list type to get the index of the element. But, only the first index could be extracted.

 "enumerate" is useful.

just to add one more python option to the above:

a = [v for v in range(len(x)) if x[v]]

will get it done in a single line. it's not the most readable thing so an explicit loop might be better depending on your purposes, but I am quite fond of the elegance of these list comprehension statements in python. 

That is certainly very elegant!  Thanks!.  I need to brush up on my list comprehension.

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