Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法执行简单的循环和列表交互来替换数组(列表列表)上的值_Python_Arrays - Fatal编程技术网

Python 无法执行简单的循环和列表交互来替换数组(列表列表)上的值

Python 无法执行简单的循环和列表交互来替换数组(列表列表)上的值,python,arrays,Python,Arrays,基本上,我想要的是在每次迭代中切换数字1的上方、下方、左侧和右侧的每个数字。像这样的事情考虑第一,和‘0号:< /P> --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | # | | | --------------------- | | | | | | --------------

基本上,我想要的是在每次迭代中切换数字1的上方、下方、左侧和右侧的每个数字。像这样的事情考虑第一,和‘0号:< /P>
---------------------
|   |   |   |   |   |
---------------------
|   |   |   |   |   |
---------------------
|   |   | # |   |   |
---------------------
|   |   |   |   |   |
---------------------
|   |   |   |   |   |
---------------------


---------------------
|   |   |   |   |   |
---------------------
|   |   | # |   |   |
---------------------
|   | # | # | # |   |
---------------------
|   |   | # |   |   |
---------------------
|   |   |   |   |   |
---------------------

---------------------
|   |   | # |   |   |
---------------------
|   | # | # | # |   |
---------------------
| # | # | # | # | # |
---------------------
|   | # | # | # |   |
---------------------
|   |   | # |   |   |
---------------------

---------------------
|   | # | # | # |   |
---------------------
| # | # | # | # | # |
---------------------
| # | # | # | # | # |
---------------------
| # | # | # | # | # |
---------------------
|   | # | # | # |   |
---------------------

---------------------
| # | # | # | # | # |
---------------------
| # | # | # | # | # |
---------------------
| # | # | # | # | # |
---------------------
| # | # | # | # | # |
---------------------
| # | # | # | # |   |
---------------------
一切都很顺利,直到我无法将数字0切换到位置4,4。 似乎找不到问题所在,我知道代码很草率,但我在做什么阻止最后一个数字改变

linhas=5

matrix =[]
for i in range(linhas):
    linha1=[]
    for j in range(colunas):
        linha1.append(0)
    matrix.append(linha1)
pl=[]
cont=0
matrix[2][2]=1
while len(pl)!= colunas*linhas:
    cont+=1
    pl=[]
    print(matrix)
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            if matrix[i][j]==1:
                lista=[i,j]
                pl.append(lista)

    for l in range(len(pl)):
        i= pl[l][0]
        j= pl[l][1]

        #this is wrong, now corrected below
        #if (j>=0 and j+1<=len(matrix)-1) and (i>=0 and i+1<=len(matrix)-1):
            #if matrix[i][j-1]==0:
                #matrix[i][j-1]=1
            #if matrix[i][j+1]==0:
                #matrix[i][j+1]=1
            #if matrix[i-1][j]==0:
                #matrix[i-1][j]=1
            #if matrix[i+1][j]==0:
                #matrix[i+1][j]=1
correction, remove # text and substitute with this:

    if j-1>=0:
        if matrix[i][j-1]==0:
            matrix[i][j-1]=1
    if j+1<colunas:
        if matrix[i][j+1]==0:
            matrix[i][j+1]=1
    if i-1>=0:
        if matrix[i-1][j]==0:
            matrix[i-1][j]=1
    if i+1<linhas:
        if matrix[i+1][j]==0:
            matrix[i+1][j]=1
j是一个列索引。i是一个行索引

对于所有有效的列索引,j>=0为真。但请注意,如果j为0,则矩阵[i][j-1]可能是矩阵[i][1]。那可能不是你想要的


j+1=0和j+1=0和i+1是的,我认为保持它在边界内的if条件是个问题,对类似的条件有什么建议吗?为4个子if语句中的每一个子if语句将其分成不同的条件。已经尝试了2个小时,似乎找不到合适的条件来强制索引保持在边界内,并且仍然包括4,4。有什么建议吗?没关系,刚刚找到了解决办法。谢谢你的回答,它真的帮助了我。