Grasshopper

algorithmic modeling for Rhino

Hi all,

I have a seemingly simple problem, but have been unable to find a solution. I have a list of 4918 items, all of which are strings (either "A," "B," or "C"). I'd like to convert this list into a list of integers of "1," "2," or "3," where all items that are "A" become 1, "B" become 2, etc. I will then associate these values with an accompanying list of lines.

Below is an image to help clarify. I've also attached the script. (Note: I left the GHPython component empty after several failed attempts at case conversion.)

Thanks in advance for your help.

Cheers,

/SPM

Views: 1492

Attachments:

Replies are closed for this discussion.

Replies to This Discussion

Hi Stephen,

You can add an integer values to a new list based on values from the strList:

a = []
for item in strList:
    if item == "A":
        newItem = 1
    elif item == "B":
        newItem = 2
    else:
        newItem = 3
    a.append(newItem)


What went wrong at your place?

Hi Djordje,

Thanks for the quick reply and for the code. I'm not sure what happened, to tell you the truth. I tried a very similar approach, but attempted to create three separate lists (one for each item type), but it didn't work out.

It makes more sense to create one master list then create sublists using another conditional loop.

Thanks again,

/SPM

Hello

i tried to do this with gh components (to practice) and thought to share it.

alex

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service