python:类型错误和索引错误(2.7.6)

python:类型错误和索引错误(2.7.6),python,list,indexing,typeerror,Python,List,Indexing,Typeerror,我是python的新手,在2.7.6上遇到了一个问题,而在3.4.4上则不是问题 守则: def answer(population, x, y, strength): # make sure Z is infectable if population[x][y] > strength: return population else: population[x][y] = -1 # get array dimentions rows = len(population)

我是python的新手,在2.7.6上遇到了一个问题,而在3.4.4上则不是问题

守则:

def answer(population, x, y, strength):

# make sure Z is infectable
if population[x][y] > strength:
    return population
else:
    population[x][y] = -1

# get array dimentions
rows = len(population)
cols = len(population[0])

# declare checking array
toCheck = []
toCheck.append([x,y])

# loop 4 way check
while(1):
    #store and pop current element
    i = toCheck[0][0]
    j = toCheck[0][1]
    toCheck.pop(0)

    # left
    if j != 0:
        if population[i][j-1] <= strength and population[i][j-1] != -1:
            population[i][j-1] = -1
            toCheck.append([i,j-1])

    # top
    if i != 0:
        if population[i-1][j] < strength and population[i-1][j] != -1:
            population[i-1][j] = -1
            toCheck.append([i-1,j])

    # right 
    if j != cols-1:
        if population[i][j+1] > strength and population[i][j+1] != -1:
            population[i][j+1] = -1
            toCheck.append([i,j+1])

    # bottom
    if i != rows-1:
        if population[i+1][j] > strength and population[i+1][j] != -1:
            population[i+1][j] = -1
            toCheck.append([i+1][j])

    if len(toCheck) == 0:
        return population
def答案(总体、x、y、强度):
#确保Z是可感染的
如果总体[x][y]>强度:
返回人口
其他:
人口[x][y]=-1
#获取数组维度
行=列(总体)
cols=len(总体[0])
#声明检查数组
toCheck=[]
toCheck.append([x,y])
#循环4路检查
而(一):
#存储并弹出当前元素
i=toCheck[0][0]
j=toCheck[0][1]
toCheck.pop(0)
#左
如果j!=0:
如果总体[i][j-1]强度和总体[i][j+1]!=-1:
人口[i][j+1]=-1
toCheck.append([i,j+1])
#底部
如果我第1行:
如果人口[i+1][j]>实力和人口[i+1][j]!=-1:
人口[i+1][j]=-1
toCheck.append([i+1][j])
如果len(toCheck)=0:
返回人口
给我一个“打字错误”。而守则:

def answer(population, x, y, strength):

# make sure Z is infectable
if population[x][y] > strength:
    return population
else:
    population[x][y] = -1

# get array dimentions
rows = len(population)
cols = len(population[0])

# declare checking array
toCheck = [[]]
toCheck.append([x,y])

# loop 4 way check
while(1):
    #store and pop current element
    i = toCheck[0][0]
    j = toCheck[0][1]
    toCheck.pop(0)

    # left
    if j != 0:
        if population[i][j-1] <= strength and population[i][j-1] != -1:
            population[i][j-1] = -1
            toCheck.append([i,j-1])

    # top
    if i != 0:
        if population[i-1][j] < strength and population[i-1][j] != -1:
            population[i-1][j] = -1
            toCheck.append([i-1,j])

    # right 
    if j != cols-1:
        if population[i][j+1] > strength and population[i][j+1] != -1:
            population[i][j+1] = -1
            toCheck.append([i,j+1])

    # bottom
    if i != rows-1:
        if population[i+1][j] > strength and population[i+1][j] != -1:
            population[i+1][j] = -1
            toCheck.append([i+1][j])

    if len(toCheck) == 0:
        return population
def答案(总体、x、y、强度):
#确保Z是可感染的
如果总体[x][y]>强度:
返回人口
其他:
人口[x][y]=-1
#获取数组维度
行=列(总体)
cols=len(总体[0])
#声明检查数组
toCheck=[]]
toCheck.append([x,y])
#循环4路检查
而(一):
#存储并弹出当前元素
i=toCheck[0][0]
j=toCheck[0][1]
toCheck.pop(0)
#左
如果j!=0:
如果总体[i][j-1]强度和总体[i][j+1]!=-1:
人口[i][j+1]=-1
toCheck.append([i,j+1])
#底部
如果我第1行:
如果人口[i+1][j]>实力和人口[i+1][j]!=-1:
人口[i+1][j]=-1
toCheck.append([i+1][j])
如果len(toCheck)=0:
返回人口
给我和“索引器”。这两个错误都发生在行i=toCheck[0][0]处。
请帮忙!谢谢。

编辑:此回答针对问题代码的旧版本

假设所有代码都应该在funk()中:

一旦toCheck为空,您需要终止while循环。在弹出所有值后,原始版本尝试为空列表编制索引。试试这个:

# loop 4 way check
while len(toCheck) > 0:
    #store and pop current element
    i = toCheck[0][0]
    j = toCheck[0][1]
    toCheck.pop(0)
另外,在添加值之前,不需要声明toCheck。你可以替换

toCheck = [[],[]]
toCheck.append([x,y])


正确设置代码缩进格式在代码中,永远不会调用
funk
,也不会提供
population
的值。请阅读并适当更新您的问题。您希望
toCheck
的状态在
toCheck.append([x,y])
之后是什么?它应该是一个两维列表,其中[x,y]是第一个元素。请不要损坏您的帖子。
toCheck = [x, y]