简体   繁体   English

Python __getitem__错误

[英]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. 经过一些研究,我发现当您尝试在一个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. 在这种情况下,我要发送3个数字的列表,然后想递归(我们不允许在第二个数字列表上使用循环:(),看看第二个列表中的任何元素是否在第一个列表中如果是,则将完成某些操作,但如果未完成,则该功能应继续执行行列表中的下一个操作,执行相同的操作,直到行为空。

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. 您正在使用numbers[num_time+1] ,而不是列表。

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. 检查您要发送的numbers ,可能是您发送的不是列表。 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] . 您得到的错误是Python告诉您要在错误的对象上使用函数的方式, attribute __getitem__正在到达诸如这些numbers[1]的列表numbers的索引。

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 . 您传递一个整数作为row_findnumbers参数。 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. 其中[1:]返回从第二个元素开始的列表。

You can also use Python boolean evaluation. 您还可以使用Python布尔值评估。 If a sequence is empty, it is evaluated to False, so instead of 如果序列为空,则将其评估为False,因此代替

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 . 对于num_lst=[5, 10, 23, 31, 44]num_lst[0] = 5num_list[0++] = 10 if "numbers" is 10, what does numbers[num_time] mean? 如果“数字”为10, numbers[num_time]是什么意思? (ie, 10[num_time] - what's that supposed to mean?). (即10[num_time] -这是什么意思?)。

instead of passing numbers[num_time+1] , as an argument back into your function, look at using slices. 而不是将numbers[num_time+1]作为参数传递回您的函数,而是使用切片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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