Python shortest walk


(Edward) #1

Something in my Python code does not work. It should give the points from the start to the end. However, the pnts_direct list is empty.

python shortest walk (12.3 KB)

(Nathan 'jesterKing' Letwory) #2

I haven’t looked at your definition yet, but looking at your code in the image one thing stands out:

The temporary list you create at the beginning of your route function is not a copy of your original list. When you modify either you modifiy both. It is not a good idea to change the list you are enumerating. You probably wanted to create a copy. Use pnts_field.copy() scratch that, py3 meddling with my mind.

(Edward) #3

Thank you for your response.
What I think is best is to copy the list, so I can work with indices.

The pnts_field is used to change the field to move over.
The pnts_field_temp is used to select the possible directions and to choose the direction from the last point of the route.
This code should be very clean. However, I do not know how to ‘straighten’ the ‘code.’

(Nathan 'jesterKing' Letwory) #4

Copy of the field list you can do with pnts_field_temp = list(pnts_field)

Further, rs.Distance() gives you a list of distances because you give it a list as the second argument. Therefor your if clause will always fail, and your for loop breaks instantly. That gives you an empty pnts_direct.

You’ll have to loop over the results in dist to do your check.

(Nathan 'jesterKing' Letwory) #5

Is this the mechanism you want?

for point str get the points proximity within distance z
    from proximity pick point c closest to point end
    save point c to pnts_direct
    if point c is point end exit loop
    else make c point str and next step in loop

return pnts_direct

(Edward) #6

Yes, that it is exactly, this pseudo code is very explanatory for what I want to make.

(Nathan 'jesterKing' Letwory) #7

line five of your code field will be a point, not a list. Line 8 then won’t do what you think it does - I am assuming you are wanting to set the element to None so it gets somehow ignored later on?

line 6 dist is not a single value, but a list of distances to all points given.

popping point in line 14 isn’t really necessary.

To create successfull scripts it is important you understand the types you are using and the functions you are using.

One way of doing this is

python shortest walk (12.0 KB)

(Edward) #8

Thank you, I appreciate your help very much.