简体   繁体   中英

Python __getitem__ error

I have a quick question, this line of code

row[time] == numbers[num_time]:

is giving me the error:

int has no attribute __getitem__

after some research, i found out that this errors occurs when you try to call a list number on an int. In this case, I am sending in a list of 3 numbers, and wanting to recurse( we arent allowed to use loops yet :( ) on the second list of numbers, seeing if any of the elements of the second list are within the first list. If they are, something will be done, but if they arent, the function should move on to the next in the row list, do the same thing, until row is empty.

def row_find(row,numbers,time,num_time):
    if numbers==[]:
         return row_find(row[time+1],numbers,time+1,num_time=0)
    if row== []:
         return row

    else:
        if  row[time]== numbers[num_time]:
            num_time=0
            return row,row_find(row[time+1],numbers,time+1,num_time)
        else:
            return row,row_find(row[time],numbers[num_time+1],time,num_time)


lst=[5,2,9]
num_lst=[5, 10, 23, 31, 44]
row_find(lst,num_lst,0,0)

Here:

row_find(row[time],numbers[num_time+1],time,num_time)

you are using numbers[num_time+1] which is not a list.

I think this should make the trick:

def row_find(row,numbers,time,num_time):
    if numbers==[]: # If numbers list is empty, it make no sense to contiue
         return False
    if row== []: # If numbers list is empty, it make no sense to contiue
         return False

    if  row[time]== numbers[num_time]: #Already found an element that is in both lists
        print("found -> " + str(time) + " " + str(num_time))
        return True
    else:
        if num_time < len(numbers)-1: # If remaining elements in numbers
            if row_find(row,numbers,time,num_time+1): # Check next one
                return True
            else: # I
                if time < len(row)-1: # If remaining element in row
                    return row_find(row,numbers,time+1,0) # check numbers from beginning with next row
                else:
                    return False # If not, no elements in both lists


lst=[8,2,9]
num_lst=[9, 10, 88, 31, 55]
row_find(lst,num_lst,0,0)
# found -> 2 0

Check what are you sending as numbers it may be that you are sending something that is not a list. The error that you get is Pythons way of telling you that you are asking to use a function on the wrong object, attribute __getitem__ is reaching the index of an list numbers like this numbers[1] .

Here:

return row,row_find(row[time],numbers[num_time+1],time,num_time)

You pass an integer as the numbers argument of row_find . You have to pass a list. Are you looking for slices ?

You want to do

row_find(row[time],numbers[1:],time,num_time)

where [1:] returns a list starting from the second element.

You can also use Python boolean evaluation. If a sequence is empty, it is evaluated to False, so instead of

if numbers==[]:

you can do:

if not numbers:

You need to do a few things:

first, check that your argument (numbers) is actually a list. add this to the top of your function:

if not isinstance(numbers, collections.Iterable):
    # error! numbers is meant to be a list. report on this error somehow

and, later in your code:

for num_lst=[5, 10, 23, 31, 44] , num_lst[0] = 5 , num_list[0++] = 10 . if "numbers" is 10, what does numbers[num_time] mean? (ie, 10[num_time] - what's that supposed to mean?).

instead of passing numbers[num_time+1] , as an argument back into your function, look at using slices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM