Typeerror - 'int' Object has no attribute '__getitem__' when iterating over list

Asked

Viewed 799 times

1

Problem: Write a function that accepts a list of integers and returns a list that is the drawn (ascending) version of the original list.(Original list should not be modified). You cannot use the built-in Sort() or Sorted function().

This is my solution:

1 def func(i):
2     i_copy = []
3     for elem in i:
4         i_copy.append(elem)
5     for y in range(0, len(i_copy)):
6         for x in i_copy:
7             if x < i_copy[y]:
8                 aux = x
9                 i_copy[y] = x
10                 i_copy = aux
11     return i_copy

Gives the following error:

Error in evaluating Function: Typeerror at line 7 int' Object has no attribute 'getitem'

Someone help me solve the problem?

I don’t understand why the attribute __getitem__ has to do with the problem or why I need it, and why this particular line is wrong.

2 answers

1

the getitem is a method that every python object has so that you can make dynamic method calls and collect attributes through a string. Ex.

>> to_upper = "string"
>> to_upper.__getattribute__("upper")()
STRING

the error of your code is when i_copy gets aux, aux is an integer, not a list, then i_copy ceases to be a list and becomes an integer, so when you try to get the next position of i_copy in if if x < i_copy[y]:, it accuses that it does not have the attribute responsible for the vector item.

Try to do so.

def func(i):
    i_copy = [x for x in i]

    for y in range(len(i)):
        for x in i_copy:
            if x < i[y]:
                i_copy[y] = x
    return i_copy
  • Thanks for your help, but it’s not right. For the following input: func ( [1256, 0, 2, -3, 4, 10, 7, 8, 9] ) should exit the following output: [-3, 0, 2, 4, 7, 8, 9, 10, 1256], but instead leaves: [9, -3, -3, -3, -3, 9, -3, -3]

  • gives a look at this sort code. https://pt.wikipedia.org/wiki/Bubble_sort

0

What happens is that the first time your program runs line 10, the array i_copy receives the value of x. However, x is just a number taken from the same array (see line 6). Then in the next loop iteration of line 6, you try to use i_copy, which has now become a number, as if it were an eternal structure, generating the exception.

All because on line 10 you forgot to reference a specific array position i_copy. And anyway the logic of your algorithm seems to be wrong. Follow my version of the same algorithm:

1   def func(i):
2       i_copy = []
3       for elem in i:
4           i_copy.append(elem)
5       for y in range(0, len(i_copy)):
6           for x in range(y + 1, len(i_copy)):
7               if i_copy[x] < i_copy[y]:
8                   aux = i_copy[x]
9                   i_copy[x] = i_copy[y]
10                  i_copy[y] = aux
11      return i_copy

Notice how on line 6 I am not iterating over each element of the vector, but only over the indices. And with these indexes I can accomplish the switching of the elements when appropriate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.