(遍历)python这道题怎么解?

(Traversal) How to solve this question in python?

所以 table 看起来像这样:

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"]))

你可以尝试这样的事情(解释添加为评论):

def traverse(tb_list):
    lst = tb_list.copy() #Takes a copy of tb_list
    lst = list(zip(*[list(elem) for elem in lst])) #Transposes the list
    final = lst[0][0] #Sets final as the first element of the list
    index = [0,0] #Sets index to 0,0

    while True:
        x = index[0] #The x coordinate is the first element of the list
        y = index[1] #The y coordinate is the second element of the list

        if x == len(lst) - 1: #Checks if the program has reached the right most point of the table
            if y == len(list(zip(*lst))) - 1: #Checks if program has reached the bottommost point of the table
                return final #If both the conditions are True, it returns final (the final string)
            else:
                final += lst[x][y+1] #If the program has reached the right most corner, but not the bottommost, then the program moves one step down
                index = [x, y+1] #Sets the index to the new coordinates

        elif y == len(list(zip(*lst))) - 1: #Does the same thing in the previous if condition button in an opposite way (checks if program has reached bottommost corner first, rightmost corner next)
            if x == len(lst) - 1:
                return final
            else:
                final += lst[x + 1][y] #If the program has reached the bottommost corner, but not the rightmost, then the program moves one step right
                index = [x + 1, y]

        else: #If both conditions are false (rightmost and bottommost)
            if lst[x+1][y] < lst[x][y+1]: #Checks if right value is lesser than the bottom value
                final += lst[x+1][y] #If True, then it moves one step right and adds the alphabet at that index to final
                index = [x+1,y] #Sets the index to the new coords
            else: #If the previous if condition is False (bottom val > right val)
                final += lst[x][y+1] #Moves one step down and adds the alphabet at that index to final
                index = [x,y+1] #Sets the index to the new coords

lst = ["veus", "oxde", "oxlx", "hwuj"]
print(traverse(lst))

输出:

veudexj

我已将说明添加为评论,请花点时间阅读。如果您对代码的任何部分仍然不清楚,请随时问我。欢迎对 optimize/shorten 我的代码提出任何建议。