正如标题所说,我一直在寻找这个问题的解决方案:
类型错误:"list"对象不可调用
有一段时间了,但我确实只找到了关于使用默认名称命名变量的答案,例如list。我认为这不是我的情况,特别是这个相关的代码:
next_matrix = 0
c = 0
routes = []
mapp_matrix = []
mapp = []
def del_nl(l):
for i in range(len(l)):
del l[i][-1]
# check if there is a valid route in a map from a starting point
def route(n_map, n_command, s_point):
for i in n_command:
if i == 'N':
s_point[0] = s_point[0] - 1
elif i == 'S':
s_point[0] = s_point[0] + 1
elif i == 'E':
s_point[1] = s_point[1] + 1
elif i == 'W':
s_point[1] = s_point[1] -1
try:
if n_map[s_point[0]][s_point[1]] != '.':
return False
except IndexError:
return False
return True
## Getting datas for every map ##
with open("/home/senso/programming/ctf_magio/input-sample.txt", "r") as f:
for line in f:
# i am reading values for the dimension of the map
if c == next_matrix:
line = line.split(" ")
int_list = [int(i) for i in line]
if not c == 0:
del_nl(mapp)
mapp_matrix.append(mapp[:])
del mapp[:]
# reading the commands
elif c == next_matrix + 1:
route = list(line)
del route[-1]
routes.append(route)
next_matrix = next_matrix + int_list[0] + 2
else:
#building the map
mapp.append(list(line))
c = c+1
del_nl(mapp)
mapp_matrix.append(mapp[:])
ship_alive = 0
for i in range(len(mapp_matrix)):
for j in range(len(mapp_matrix[i])):
for z in range(len(mapp_matrix[i][j])):
#oh my god is O(n^3), will it explode??
if mapp_matrix[i][j][z] == '.':
point = [j, z]
if route(mapp_matrix[i], routes[i], point):
ship_alive = 1 + ship_alive
我得到这个错误
文件"capitanSonar.py",第78行,位于如果路由(mapp_matrix[i],routes[i],point):TypeError:"list"对象不可调用
如果我尝试用静态列表调用"route",它会起作用。
感谢您的帮助。
# reading the commands
elif c == next_matrix + 1:
route = list(line) # <- Here. Pick a different name for the list
del route[-1]
routes.append(route)
next_matrix = next_matrix + int_list[0] + 2
else:
#building the map
mapp.append(list(line))
c = c+1