Hi,
I am writing a simple code which loops through my 2 lists, check the values in one list and if the condition is true, returns the corresponding value from 2nd list. (attached below is a snippet of the code)
When creating code the level of indentation is important, each time the code iterates over the zipped values the code overwrites the out variable at line 7.
If you want a list of the stations where the offset is greater than or equal to 1 use the append function
station = IN[0]
offset = IN[1]
result = []
for stn, off in zip(station, offset):
if off >= 1:
result.append(stn)
OUT = result
Hi,
I have one more query, this is a code i am writing where i give two inputs of thickness and places and then distribute thickness in number of places based on certain conditions.
When i give direct inputs in my code, it works but while i connect two lists having multiple values of thickness and places, it shows some error. More multiple values of thickness and places, i should get multiple lists of distributed thicknesses.
Please check once.
import math
remaining_thickness = IN[0]
remaining_places = IN[1]
def calculate_resin_thickness(remaining_thickness, remaining_places):
result_array = [‘’] * remaining_places
t = math.floor(remaining_thickness / remaining_places / 2.5) * 2.5
if remaining_places >= 2:
result_array[0] = t
result_array[1] = t
x = remaining_places
y = remaining_thickness
i = 2
while i < remaining_places - 2:
remaining_thickness -= 2 * t
x -= 2
division_value = remaining_thickness / x
t = math.floor(division_value / 2.5) * 2.5
if result_array[i - 1] - t > 2.5:
result_array[i - 1] -= 2.5
result_array[i - 2] -= 2.5
remaining_thickness += 5
division_value = remaining_thickness / x
t = math.floor(division_value / 2.5) * 2.5
result_array[i] = t
result_array[i + 1] = t
i += 2
else:
result_array[0] = t
Calculate the sum of elements in the result_array
total = sum(map(float, result_array[:-2]))
Adjust the last two elements of result_array to satisfy the remaining thickness
z = y - total
t = z / 2
if result_array[-3] - t > 2.5:
result_array[-3] -= 2.5
result_array[-4] -= 2.5
z += 5
t = z / 2
result_array[-2] = str(t)
result_array[-1] = str(t)
return result_array
Example usage:
thickness = calculate_resin_thickness(remaining_thickness,remaining_places)
OUT = thickness
import math
remaining_thickness = IN[0]
remaining_places = IN[1]
def calculate_resin_thickness(remaining_thickness, remaining_places):
result_array = [0] * remaining_places
newlist = []
t = math.floor(remaining_thickness / remaining_places / 2.5) * 2.5
if remaining_places >= 2:
result_array[0] = t
result_array[1] = t
x = remaining_places
y = remaining_thickness
i = 2
while i < remaining_places - 2:
remaining_thickness -= 2 * t
x -= 2
division_value = remaining_thickness / x
t = math.floor(division_value / 2.5) * 2.5
if result_array[i - 1] - t > 2.5:
result_array[i - 1] -= 2.5
result_array[i - 2] -= 2.5
remaining_thickness += 5
division_value = remaining_thickness / x
t = math.floor(division_value / 2.5) * 2.5
result_array[i] = t
result_array[i + 1] = t
i += 2
else:
result_array[0] = t
# Calculate the sum of elements in the result_array
total = sum(map(float, result_array[:-2]))
# Adjust the last two elements of result_array to satisfy the remaining thickness
z = y - total
t = z / 2
if result_array[-3] - t > 2.5:
result_array[-3] -= 2.5
result_array[-4] -= 2.5
z += 5
t = z / 2
result_array[-2] = str(t)
result_array[-1] = str(t)
return newlist.append(result_array)
# Example usage:
thickness = calculate_resin_thickness(remaining_thickness,remaining_places)
OUT = thickness
Hi Ritesh,
Further to @pyXam 's comment would also familiarise yourself with this post How to get help on the Dynamo forums
Finally - it is preferable to start a new topic rather than add to the end of a solution
It would help with a description of the use case or context and what you are trying to achieve rather than providing a code dump. That way you are more likely to get help. I am almost certain that this can be simplified a lot.
Here are some observations
t = math.floor(division_value / 2.5) * 2.5 is used twice in the function - create a helper function at the top like this. Use floor division // instead of math.floor
def calculate_resin_thickness(remaining_thickness, remaining_places):
def conform_thickness(rt, rp):
return rt / rp // 2.5 * 2.5 # No imports required using // floor division
result_array = [0] * remaining_places
There is a potential to handle edge cases - say remaining_places = 1
if remaining_places = 1:
return [remaining_thickness]
Is there a reason why you are adding strings to the result?
You can handle individual values and lists with something like this
remaining_thickness = IN[0] if isinstance(IN[0], list) else [IN[0]]
remaining_places = IN[1] if isinstance(IN[1], list) else [IN[1]]
thickness = [
calculate_resin_thickness(*t_and_p)
for t_and_p in zip(remaining_thickness, remaining_places)
]