我想练习一下递归,并认为在不规则矩阵中搜索会很有趣。我有一个有效的简单递归,但不知何故并没有完成整个数组。它找不到数字 8-11。它需要返回匹配的坐标,以便我以后可以使用它。我错过了什么吗?
array = [
[1, 2, [3, 4, [5, [6, [7, 0]]]], [8, 9], 10, 11]
]
def searchArray(array, searched, coordinates=[]):
for i, dimension in enumerate(array):
if type(dimension) == int:
if dimension == searched:
return coordinates+[i,]
else:
coordinates.append(i)
return searchArray(dimension, searched, coordinates=coordinates)
searched = 5
coordinates = searchArray(array, searched)
print(coordinates)
慕工程0101907
相关分类