# Python - Sum Of List Of Values (Converted To Model Units)

Hello,

I’ve made this python script that converts values from specified units to desired units and it’s working as expected with the exception of the S output.

I would like this output to return the Sum of the R list results.

I realize this is a pretty basic question but I can’t figure it out or find a working example of how to achieve this. (Python newbie, sorry)

Thanks for your help & guidance!

Script:

``````V = float(V)
C = [C]

conversion_functions = {
"in_ft": lambda V: V / 12,
"ft_in": lambda V: V * 12,
"m_ft": lambda V: V / 0.3048,
"cm_ft": lambda V: V / 30.48,
"cm_in": lambda V: V / 2.54,
"mm_ft": lambda V: V / 0.0328084,
"ft_m": lambda V: V * 0.3048,
"ft_cm": lambda V: V * 30.48,
"ft_mm": lambda V: V * 304.8,
"in_m": lambda V: V * 0.0254,
"in_cm": lambda V: V * 2.54,
"in_mm": lambda V: V * 25.4,
}

Results = []

for i in C:
if i in conversion_functions:
R = conversion_functions[i](V)
Results.append(R)
print Results
else:
R = V

S = sum(Results)
``````

Graph Space:

Well, you might want to check out the rhinoscriptsyntax built-in unit scaling methods:

`rs.UnitSystem()` will get you the current file units, *and allow you to change them and scale the geometry or not.
https://developer.rhino3d.com/api/RhinoScriptSyntax/#document-UnitSystem

`rs.UnitScale()` will give you the scale factor for converting between unit systems if you just need that.
https://developer.rhino3d.com/api/RhinoScriptSyntax/#document-UnitScale

Both work in the GH Python component… *but I’m not sure about actually changing the Rhino document unit setting.

2 Likes

``````functions = {
"in_ft": lambda V: V / 12,
"ft_in": lambda V: V * 12,
"m_ft": lambda V: V / 0.3048,
"cm_ft": lambda V: V / 30.48,
"cm_in": lambda V: V / 2.54,
"mm_ft": lambda V: V / 0.0328084,
"ft_m": lambda V: V * 0.3048,
"ft_cm": lambda V: V * 30.48,
"ft_mm": lambda V: V * 304.8,
"in_m": lambda V: V * 0.0254,
"in_cm": lambda V: V * 2.54,
"in_mm": lambda V: V * 25.4,
}

Results = []

for v, c in zip(V,C):
if c in functions:
Results.append(functions[c](v))
else:
Results.append(v)

R = Results
S = sum(Results)
``````

Michael.gh (5.1 KB)

1 Like

Thanks @Mahdiyar,

However, I’m running across the same error as I was earlier when trying to summate the loop values.

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

Traceback:
line 28, in script

Is this telling us that the variable needs to be a list and it is reading it as a single value?

I set the input to List Access but get the error regardless.

Thanks @Helvetosaur,

Here’s what I came up with after your suggestion (I like how this is simplified from my previous example):

Graph Spaces:

Python:

``````import rhinoscriptsyntax as rs

F = int(F)
V = float(V)

CS = rs.UnitSystem()
SF = rs.UnitScale(CS,F)

print CS
print SF

R = SF*V
print R
``````

Any tips for returning the R as the sum of the two values?

Thank you!

Alright I figured it out with many a chatGPT ring around the rosie.

Posting here in case anyone is interested:

``````import rhinoscriptsyntax as rs

CS = rs.UnitSystem()
SF = [rs.UnitScale(CS, f) for f in F]

# Scale each V value by the corresponding SF value
R = [sf * v for sf, v in zip(SF, V)]

S = sum(R)
``````

Graph Space:

Thank you all for your help!!

Not to be that guy, but finding the `sum()` function is a Google away:

Or more directly yet, search the Python docs:

1 Like

No for sure you are right! I was trying that but my issue was that I was trying to iterate over a float and couldn’t figure out how to make it work with a list.

So when I attempted sum and math.fsum at one point I kept getting the error about float not being iterable.

That was why I needed the loop logic and inputs to give list access.

I’m still trying to wrap my head around a lot of python basics and I appreciate the help and counter points. Thanks @AndersDeleuran

1 Like

Ah yes, sorry, I just returned to work after a long sick leave and think I was a bit triggered by the sudden ChatGPT idiocracy of it all. Apologies for the knee jerking and happy Pythoning No worries at all! I mostly leverage ChatGPT to explain errors to me and ask it if there are ways to optimize the code or different approaches.

It certainly isn’t a google replacement and definitely doesn’t know how to create any complex code (yet) but for a newbie like me it’s been really helpful to have it generate syntax for me that I can learn from or asking it about options for different functions.

and thank you! 1 Like