traverse(["veus", "oxde", "oxlx", "hwuj"])
返回"veudexj"
因此,该表将如下所示:
v o o h
e x x w
u d l u
s e x j
我是python新手,我写了这段代码……但它只打印"veuexj"
我想说问题出在这一行if new_list[a - 1][b - 1] == new_list[a - 1][-2]:
,这迫使参数跳过'd'
字符。我不知道如何解决它
def traverse(tb_list):
new_list = tb_list.copy()
st = new_list[0][0]
parameter = ''
handler = 1
for a in range(1, len(new_list)):
for b in range(handler, len(new_list[a])):
if new_list[a - 1][b - 1] == new_list[a - 1][-2]:
parameter = new_list[a][b]
elif new_list[a - 1][b - 1] > min(new_list[a - 1][b], new_list[a][b - 1]):
parameter = min(new_list[a - 1][b], new_list[a][b - 1])
elif new_list[a - 1][b - 1] < min(new_list[a - 1][b], new_list[a][b - 1]):
parameter = min(new_list[a - 1][b], new_list[a][b - 1])
st += parameter
handler = b
return st
print(traverse(["veus", "oxde", "oxlx", "hwuj"]))
您可以尝试这样的方法(解释添加为注释):
输出:
我已将解释添加为注释,因此请您慢慢看一看。如果您仍然不清楚代码的任何部分,请随时询问我。欢迎提出任何优化/缩短我的代码的建议
相关问题 更多 >
编程相关推荐