Reputation: 181
I wanna read a list of number and sort them functionally, and here is the code:
user_input = [int(i) for i in input().split(' ')]
for i in user_input:
if i == user_input[0]: list2 = [[i]]
for j in list2:
if user_input[i] - j[-1] == 1:
j.append(user_input[i])
else:
list2.append(user_input[i])
print(list2)
#############################################
>>> 8 7 1 9 2 6 3 5 4
Traceback (most recent call last):
File "test.py", line 6, in <module>
if user_input[i] - j[-1] == 1:
TypeError: 'int' object is not subscriptable
I googled this problem and know that this problem happened when call int object with subscript, but when I run print(type(user_input), type(j))
and both the result are list
. I wonder why the error happened. Can someone explain me please:)
Upvotes: 1
Views: 59
Reputation: 184
its a bad idea to modify your list2 while iterate through them. your list2 starts as a list of list, then you starts to insert int to it, which causes the problem...
Upvotes: 5