-1

So I want to sort a list of namedtuples using insertion sort in python, and here is my code.

    def sortIt(tripods):
        for i in range(1, len(tripods)):
            tmp = tripods[i][3]
            k = i
            while k > 0 and tmp < tripods[k - 1][3]:
                tripods[k] = tripods[k - 1]
                k -= 1
            tripods[k] = tmp

Whenever it runs it gives me a TypeError: int object is not suscriptable. Tripods is a list of namedTuples and the index 3 of the namedTuple is what I want the list to be ordered by.

2
  • What type of object does tripods[k] = tmp assign to tripods[k]? What will happen next time you examine tripods[k]? Commented Sep 12, 2016 at 22:27
  • Possible duplicate of Python sort a list with external key Commented Sep 12, 2016 at 22:27

1 Answer 1

0
def sortIt(tripods):
    for i in range(1, len(tripods)):
        tmp = tripods[i]
        k = i
        while k > 0 and tmp[3] < tripods[k - 1][3]:
            tripods[k] = tripods[k - 1]
            k -= 1
        tripods[k] = tmp

I have modified your code. You were assigning tripods[i][3] to tmp, which is a int. That's why the compiler warns you int is not suscriptable.

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.