Home > OS >  TypeError in my code. Using selection sort to sort values at a certain index
TypeError in my code. Using selection sort to sort values at a certain index

Time:05-09

I want my selection sort to sort the values in a certain column (index 3) however I get this message when I run my code.

The error I receive is:

https://i.stack.imgur.com/dxG2G.png

def selectionSort(lst2):
  size = len(lst2)
  for i in range(size):
    min_idx = i[3]
    for j in range(min_idx   1, size):
      if lst2[j] < lst2[min_idx]:
        min_idx = j
    lst2[i[3]], lst2[min_idx] = lst2[min_idx], lst2[i[3]]

lst2 = lst
selectionSort(lst2)
print(lst2)

CodePudding user response:

First, it is much better to copy/paste your error text into the question so that it is immediately visible and also searchable for future users of the questions.

To understand the error message "'int' object is not subscriptable:"

  • The 'int' object here is i
  • [3] is the subscript, which is being applied to i in i[3]
  • So, the error is telling you that you've tried to find the 4th element within i, but i is just an integer and there are no subelements.
  • Related