Python script does not like the list

Hello,
I’m trying to rig this script:

But If I’m using stations from previous lists-the script is not working:


If I create new list with similar values, it is working fine:

Any ideas? Thanks :slight_smile:
EDIT.1: In PyCharm it is working fine with similar values.

To be able to assist please provide all the inputs with pinned dropdowns
Also provide the code as text rather than a screen shot using </> from the format menu
If you use triple backticks ```python and close with ``` it will format for python syntax

arc_stations = [[0, 10, 20, 30], [210, 220, 230, 240]]
arc_ws = [2.33, 2.33]
spiral_stations = [[140, 150, 160, 170], [490, 500, 510, 520]]
spiral_w_increasing = [0.29, -0,29]
stations = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 9000]

speed_at_arc_station = []
for values, multiplier in zip(arc_stations, arc_ws):
    saas_map = {value: round(multiplier * (i + 1), 2) for i, value in enumerate(values)}
    speed_at_arc_station.extend(saas_map.items())

mapped_arcs = [{k: v} for k, v in speed_at_arc_station]

speed_at_spriral_station = []

for values, multiplier in zip(spiral_stations, spiral_w_increasing):
    saps_map = mappings = {value: round(abs(multiplier * (i + 1)), 2) for i, value in enumerate(values)}
    speed_at_spriral_station.extend(saps_map.items())

mapped_spirals = [{k: v} for k, v in speed_at_spriral_station]

mapped_alignment_elements = {}

for mapping in mapped_arcs:
    for key, value in mapping.items():
        key = str(key)
        value = float(value)
        mapped_alignment_elements[key] = max(mapped_alignment_elements.get(key, float('-inf')), value)

for mapping in mapped_spirals:
    for key, value in mapping.items():
        key = str(key)
        value = float(value)
        mapped_alignment_elements[key] = max(mapped_alignment_elements.get(key, float('-inf')), value)

s_w = [{k: v} for k, v in mapped_alignment_elements.items()]

w_list = []

for station in stations:

    station_str = str(station)

    if station_str in mapped_alignment_elements:
        w_list.append(mapped_alignment_elements[station_str])

    else:
        w_list.append(0)
print(w_list)
OUT = w_list

It is exactly the same as in Dynamo. Values are similar, not the orginal, because they are more than 20000. Here it is working fine, but If I use it in Dynamo, I’m getting only [0, 0, 0, … ]

EDIT.1: I tried only with station variable from dynamo and I still get the problem.

EDIT.2: Even with same values, but when I exported them to excel and then created new list, it is working…

EDIT.3:This code block is making the issue. No matter If I round it, it makes issue.

If I manually add values, instead ot writting a…b…c it works fine.

I ran you code in Dynamo Sandbox and had no issue

The code is doing a lot of gymnastics and unnecessary processing to achieve your outcome
Breaking it down I see that you want to calculate the speed_at_arc and speed_at_spiral and pick the maximum value at each station

This line is stopping the code station_str = str(station) as "10" does not equal "10.12"
If all the stations are more than a few meters apart use ``station_str = str(int(station))`

Mismatching list lengths zip will only take the first two items from spiral_w_increasing

spiral_stations = [[140, 150, 160, 170], [490, 500, 510, 520]]
spiral_w_increasing = [0.29, -0, 29]

Although I would try and align inputs, working with your inputs and using the station distance integer as a key - sample code below

arc_stations = [[0, 10, 20, 30], [210, 220, 230, 240]]
arc_ws = [2.33, 2.33]
spiral_stations = [[140, 150, 160, 170], [490, 500, 510, 520]]
spiral_w_increasing = [0.29, 0.48]
stations = IN[0]
stations = list(map(int, stations))

mapped_arcs = dict(
    (station, round(abs(mult * i), 2))
    for stations, mult in zip(arc_stations, arc_ws)
    for i, station in enumerate(stations, 1)
)
mapped_spirals = dict(
    (station, round(abs(mult * i), 2))
    for stations, mult in zip(spiral_stations, spiral_w_increasing)
    for i, station in enumerate(stations, 1)
)

w_list = [
    max(mapped_arcs.get(station, 0), mapped_spirals.get(station, 0)) for station in stations
]

OUT = w_list

Note: Python will accept integers and floats as dictionary keys, Dynamo does not
Edit: Code clean up

I had conceptual problems with that code, so I rewrite it.

arc_stations = IN[0]
arc_ws = IN[1]
spiral_stations = IN[2]
spiral_w_increasing = IN[3]
stations = IN[4]

w_list = [0] * len(stations)

def fill_speeds_constant(station_set, multiplier, stations, w_list):
    for values, m in zip(station_set, multiplier):
        for station in values:
            if station in stations:
                index = stations.index(station)
                w_list[index] = round(m, 2)

def fill_speeds_incremental(station_set, multiplier, stations, w_list):
    for values, m in zip(station_set, multiplier):
        for i, station in enumerate(values):
            if station in stations:
                index = stations.index(station)
                if m > 0:
                    w_list[index] = round(m * (i + 1), 2)
                else: 
                    w_list[index] = round(abs(m) * (len(values) - i), 2)

fill_speeds_constant(arc_stations, arc_ws, stations, w_list)
fill_speeds_incremental(spiral_stations, spiral_w_increasing, stations, w_list)

OUT = w_list

Now I have to find time to remove all nodes and put them in script… But this is my first week in python and I have a lot of thing to learn…

Actually that is what I’m chasing:


This green line represents the speed of the train.

Thank you Mike!