0

Making a recursive function to sort the graph points. When compiling get the error:

TypeError: can only concatenate list (not "NoneType") to list

Sample xy values: [[1, 1], [2, 2], [2, 3], etc.]

def FindNext(list_xy):
    if len(list_xy) == 1:
        xo = list_xy[0][0]
        yo = list_xy[0][1]
        return [xo, yo]
    xo = list_xy[0][0]
    yo = list_xy[0][1]
    for i in range(len(list_xy)):
        xf = list_xy[i][0]
        yf = list_xy[i][1]
        dx = xo - xf
        dy = yo - yf
        d = (dx**2+dy**2)**0.5
        if d == 2**0.5 or d ==1:
            return [xo, yo] + FindNext(list_xy[1:])
FindNext(xy)
5
  • 2
    Not all paths in FindNext return a value. Commented Mar 16, 2021 at 17:56
  • how do you sort the points, by x, by y, etc.? Commented Mar 16, 2021 at 17:56
  • Your sample xy works for me when I run your code. Which line is the error supposedly on? Commented Mar 16, 2021 at 17:58
  • Please provide a runnable minimal reproducible example. Commented Mar 16, 2021 at 17:59
  • Full data xy data set: [[1, 1], [2, 2], [2, 3], [2, 4], [2, 5], [3, 6], [4, 6], [5, 6], [6, 7], [7, 7], [8, 7], [9, 8], [7, 9], [9, 9], [6, 10], [8, 10], [4, 11], [5, 11], [3, 12], [8, 12], [9, 12], [4, 13], [6, 13], [7, 13], [10, 13], [5, 14], [9, 14], [9, 15], [9, 16], [8, 17], [9, 18], [9, 19], [13, 19], [10, 20], [11, 20], [12, 20], [14, 20], [15, 21], [6, 22], [10, 22], [12, 22], [15, 22], [5, 23], [7, 23], [8, 23], [9, 23], [11, 23], [13, 23], [16, 23], [5, 24], [13, 24], [16, 24], [4, 25], [14, 25], [15, 25], [3, 26], [4, 27], [3, 28], [3, 29], [3, 30], [2, 31]] Commented Mar 16, 2021 at 18:44

1 Answer 1

1

Python implicitly returns None if it reaches the end of a function without hitting a return, so one of your function inputs is probably doing that. You can solve for that by adding return [] to the end of the function, though I don't know if the logic is correct for what you're calculating.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.