我仍然是 Python 的新手,所以请容忍我糟糕的语法和逻辑,如果有的话。无论如何,我有一个函数,我正试图干净地(请不要花哨的动作)打破递归循环。它是程序中的一个函数,它递归地遍历 1 和 0(参见下面的输入文件),并将相邻的 0 标识为不同的子集。我有一个名为“checkAllInOneDirection”的递归函数,它将遍历每个位置,向右、向左、向上、向下移动以检查 0。(对于每个递归,它在 4 个方向中的每一个上只剩下一个深度/更远)。
问题是由于某种原因,第三组的输出应该仅将 0,9 和 0,10 检测为一个不同的集合,但是当它在第二组检测后脱离递归时,它拾取 [0, 4] 和 [1 , 3] 在第三组检查的开始...有什么帮助吗?
这是输出 [行,列]:
Distinct subset found : [[0, 0]]
Distinct subset found : [[0, 3], [0, 4], [1, 3], [0, 5], [1, 4], [1, 5]]
Distinct subset found : [[0, 9], [0, 4], [1, 3], [0, 10]]
正确的第三个子集应该只有:
Distinct subset found : [[0, 9], [0, 10]]
这是一个示例输入文件:
01100011100100000
11100011111111011
10011111101011011
这是该函数的一个片段,它被称为“checkAllInOneDirection”:
isItLast = checkLast(forBoo, bakBoo, upBoo, dwnBoo)
if isItLast:
for each in tempCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch=[]
for each in newCatch:
if not each in finalCatch:
finalCatch.append(each)
newCatch=[]
return finalCatch, newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo
else:
for each in tempCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch =[]
for each in newCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch.append(each)
newCatch = []
return checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo)
这是整个功能,希望它只是澄清而不是让我的问题更加混乱:
def checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo):
for each in range (0, len(tempCatch)):
posToCheck = posToCheckBak = posToCheckUp = posToCheckDwn = [tempCatch[each][0], tempCatch[each][1]]
newPosForward = checkForward(posToCheck, width)
if newPosForward != False:
tempLocale = locale[newPosForward[0]][newPosForward[1]]
elif newPosForward == False:
tempLocale = 1
if newPosForward != False and tempLocale ==0 and not newPosForward in finalCatch and not newPosForward in newCatch:
forVal = locale[newPosForward[0]][newPosForward[1]]
newCatch.append(newPosForward)
posToCheck = newPosForward
forBoo = True
elif newPosForward == False and tempLocale == 1 and not newPosForward in newCatch:
forBoo = False
newPosBackward = checkBackward(posToCheckBak)
if newPosBackward != False:
tempLocale = locale[newPosBackward[0]][newPosBackward[1]]
elif newPosBackward == False:
tempLocale = 1
if newPosBackward != False and tempLocale ==0 and not newPosBackward in finalCatch and not newPosBackward in newCatch:
forVal = locale[newPosBackward[0]][newPosBackward[1]]
newCatch.append(newPosBackward)
posToCheckBak = newPosBackward
bakBoo = True
elif newPosBackward == False and tempLocale == 1 and not newPosBackward in newCatch:
bakBoo = False
newPosUp = checkUpRow(posToCheckUp)
if newPosUp != False:
tempLocale = locale[newPosUp[0]][newPosUp[1]]
elif newPosUp == False:
tempLocale = 1
if newPosUp != False and tempLocale ==0 and not newPosUp in finalCatch and not newPosUp in newCatch:
forVal = locale[newPosUp[0]][newPosUp[1]]
newCatch.append(newPosUp)
posToCheckUp = newPosUp
upBoo = True
elif newPosUp == False and tempLocale == 1 and not newPosUp in newCatch:
upBoo = False
newPosDwn = checkDwnRow(posToCheckDwn, height)
if newPosDwn != False:
tempLocale = locale[newPosDwn[0]][newPosDwn[1]]
elif newPosDwn == False:
tempLocale = 1
if newPosDwn != False and tempLocale ==0 and not newPosDwn in finalCatch and not newPosDwn in newCatch:
forVal = locale[newPosDwn[0]][newPosDwn[1]]
newCatch.append(newPosDwn)
posToCheckDwn = newPosDwn
dwnBoo = True
elif newPosDwn == False and tempLocale == 1 and not newPosDwn in newCatch:
dwnBoo = False
isItLast = checkLast(forBoo, bakBoo, upBoo, dwnBoo)
if isItLast:
for each in tempCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch=[]
for each in newCatch:
if not each in finalCatch:
finalCatch.append(each)
newCatch=[]
return finalCatch, newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo
else:
for each in tempCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch =[]
for each in newCatch:
if not each in finalCatch:
finalCatch.append(each)
tempCatch.append(each)
newCatch = []
return checkAllInOneDirection(finalCatch,tempCatch,recursiveCount,newCatch, columnCount, rowCount, width, height, posToCheck, forBoo, bakBoo, upBoo, dwnBoo)