How to convert a string Integer number into a Dynamo number feeding a list of strings with Python?

How to convert a string number into a Dynamo number feeding a list of strings with Python? I do not want to see any warning in that operation or rebuild the lists to feed only a string that is a number.

You can do something like this without Python:
image

2 Likes

Or something like this with Python:

toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = []

for li in toList(IN[0]):
	out = []
	for x in li:
		try:
			out.append(float(x))
		except:
			out.append(x)
	OUT.append(out)

I agree but I would have to add maybe something more like this after the lis.Map, but result is a Double, not an Integer number type

1 Like

@ruben.romero If you need to maintain the numbers as int and float then try this:

import ast
toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = []

for li in toList(IN[0]):
	out = []
	for x in li:
		try:
			out.append(ast.literal_eval(x))
		except:
			out.append("Ruben")
	OUT.append(out)

many thanks, I tried both python scripts and I do not get what I expected. I need a number but Integer not Double object type, forgot to mention that. In first python the result gets same identical list as original, the second splits the commas of the string number and converts to integers, so I can see 2 results for the number

@ruben.romero Try this:

import locale
locale.setlocale(locale.LC_ALL, 'fr_FR')

toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = []

for li in toList(IN[0]):
	out = []
	for x in li:
		try:
			out.append(int(locale.atof(x)))
		except:
			out.append("Ruben")
	OUT.append(out)

that is great!, getting the integer numbers, but also I would like to keep the original strings as same :sweat_smile:

@ruben.romero Just change “Ruben” to x

import locale
locale.setlocale(locale.LC_ALL, 'fr_FR')

toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = []

for li in toList(IN[0]):
	out = []
	for x in li:
		try:
			out.append(int(locale.atof(x)))
		except:
			out.append(x)
	OUT.append(out)
2 Likes

many thanks, absolutely great. Also I edited the script in case I need a float number (double number) like this:

import locale
locale.setlocale(locale.LC_ALL, 'fr_FR')

toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = []

for li in toList(IN[0]):
	out = []
	for x in li:
		try:
			out.append(float(locale.atof(x)))
		except:
			out.append(x)
	OUT.append(out)

I guess if you change the locale language different than the French, the number conversion may change

float(locale.atof(x)) wouldn’t be required since locale.atof(x) outputs a float.
image

1 Like

@AmolShah or maybe @ruben.romero?
What do i have to change in the Python to get a Flattened list?
So i don’t need the List.Flatten node after it? Is that possible?
I also noticed it Splits number in two. So 23 become a 2 and 3. I need it to stay 23.

image

@bvs1982 Why not just use the String.ToNumber OOTB node in that case?

Or you can do something like this with Python:
image

And if your language is not French then just OUT=float(IN[0]) should do the trick.

@AmolShah

Valid question. My graph is for use in the Dynamo player and the String can be either a letter or a number. But the number needs to be integer later in my graph. I think the Python solution was pretty neat and makes the graph less cluttered (instead of this :point_down:).

2 Likes