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

1 Like

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

@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