Grasshopper

algorithmic modeling for Rhino

Converting a list of strings to a list of floats using GhPython

Hi everyone

I need to convert a list of strings into a list of floats but so far I am frankly getting very confused.

The 1st picture shows what I am trying to do 

The 2nd shows the code within the component Convert to list floats how I convert strings to floats

The 3rd shows how I take the output from the component seen in 2 and I use another python component to check that it is a float - the result I get is that they are not floats even though they should have been converted to floats

The 4th shows the code in the component seen in the 3rd.

It even though the code in the 2nd component works, it turns out that the numbers that the 2nd component is putting out are NOT floats. Can anyone help me with what is going on here? 

Ive also attached the Grasshopper file

Views: 1695

Attachments:

Replies to This Discussion

Hi Anton,

You did not directly supply your second ghpython component with the "b" output of the first one, but instead you supplied the intermediate panel with it, and then you plugged it into your second ghpython component. In this way all your float numbers from first ghpython component got converted to strings when they "entered" the panel.
If you still want to have panel input, then you have to change the second ghpython component's "x" input parameter's "Type Hint" to "float".
Other than that, you forgot to set the "x" input of second ghpython component to "List access" and the syntax also needed to me altered a bit. Check the attached file.

Attachments:

Thank you Djordje this fixed it! 

Hi Anton,

1. Right click on the input x and set the type to float, otherwise GHPython assumes it as a string. That's why you get False in the last image.

2. You can use map: ListOfFloats = map(float, ListOfStrings)

Hope it helps,

Mostapha

Mostapha could you please tell me in a bit more detail how map works 

when I do,

a = map(float, x)

where a is a list out of the python component and x is the a list of strings in I get the error

Runtime error (TypeErrorException): float is not iterable
Traceback:

If possible could you make a quick example file?

Float is just a single number. Perhaps you need to use float[]?

If you're willing to do the iteration yourself, you can also use GH_Convert.ToDouble. It treats the string as a potential expression so it'll work for strings like "pi*2" as well.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service