extracting numeric values from a list of parameters
Hi all,
a simple but for me unsolveble question.
i have a list of parameters that show the glass area of the windows contained in some rooms.
i need to convert the list of parameter to a list of numbers and sum the areas of the windows contained in each room.
the final output that i need is the total area of glazed surface in each room (contained in a list of NUMBERS) that i can compare whit other numbers whit the “>=” function.
# Importing the necessary libraries
import clr
import re
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
def sum_integers(input_list):
"""Sum integers from a list of lists of strings.
Args:
input_list (list): A list of lists of strings.
Returns:
list: A list of sums of integers for each sublist.
"""
output_list = []
for sublist in input_list:
int_sum = 0
for item in sublist:
# Extract numbers from the string
numbers = re.findall(r'\d+', item)
for num in numbers:
try:
int_item = int(num)
int_sum += int_item
except ValueError:
continue
output_list.append(int_sum)
return output_list
# The input to this node will be stored in the IN variable.
data_entering_node = IN
# Assign your output to the OUT variable.
OUT = sum_integers(data_entering_node[0])
Or an alternative solution using the GeniusLoci package
just tried your way copying the code in python but give me this error.
could be due to the fact that the list is composed by parameters and not strings, like in your exemple whit the code block?
how can i solve it?
Ah ok yes, there are 2 errors here.
In your example shown they were strings so that is the way that i thought it was being input.
To solve this you can simply put a String from Object node before the python node (Whilst i’m sure there is an elegant python solution to this i can’t remember what that is off my head and its nearly midnight here so yeah na…
Secondly the example script is expected to have Floats and not intergers as i was expecting.
This one i could solve easily enough, the revised code is below.
Unfortunatly, if this doesn’t work i won’t be able to help until tomorrow my time.
# Importing the necessary libraries
import clr
import re
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
def sum_numbers(input_list):
"""Sum numbers from a list of lists of strings.
Args:
input_list (list): A list of lists of strings.
Returns:
list: A list of sums of numbers for each sublist.
"""
output_list = []
for sublist in input_list:
num_sum = 0
for item in sublist:
# Extract numbers from the string
numbers = re.findall(r'[-+]?[0-9]*\.?[0-9]+', item)
for num in numbers:
try:
float_item = float(num)
num_sum += float_item
except ValueError:
continue
output_list.append(num_sum)
return output_list
# The input to this node will be stored in the IN variable.
data_entering_node = IN
# Assign your output to the OUT variable.
OUT = sum_numbers(data_entering_node[0])
Show us the rest of the graph. It looks like you’re getting the parameter itself, which also shows the value in Dynamo. If that’s the case, you can just get the parameter value directly instead with GetParameterValueByName.