简体   繁体   English

有没有办法以 python 中的特定索引顺序遍历列表

[英]Is there a way to iterate through a list in a specific order of indexes in python

Okay so it has been a long time since I've worked in python.好的,自从我在 python 工作以来已经有很长时间了。 But basically I am doing the classic 8-puzzle problem so given a string such as "12-453786" my program solves it for the desired "12345678-".但基本上我正在做经典的 8 拼图问题,所以给定一个字符串,例如“12-453786”,我的程序将它解决为所需的“12345678-”。 I am using breadth-first search to solve it currently and am storing visited nodes and the node the came from in the below example.我目前正在使用广度优先搜索来解决它,并在下面的示例中存储访问过的节点和来自的节点。 However to trace the path or the amount of moves it actually takes to complete the puzzle I need to be able to start at the solved tuple and trace my way back through my list of tuples to the start state但是,要跟踪完成谜题所需的路径或移动量,我需要能够从已解决的元组开始,并通过我的元组列表追溯到开始 state

I was considering doing some sort of while solved.= startstate loop but that wont exactly work.我正在考虑做某种 whilesolved.= startstate 循环,但这不会完全奏效。

 def breadth_first_search(puz):
    qu = queue.Queue()
    laststate=""
    qu.put((puz, laststate))
    startstate=puz
    visited=[]
    #visited[puz] = puz

    while queue: 

        puz = qu.get()
        visited.append(puz)

        pos = puz[0].index('-')
        nebs = neighborcells(pos)
        #print(*visited)
        if puz[0] == "12345678-":

            break
        else:
            for i in nebs:

                swapped=swap(puz,i,pos)
                if swapped in visited:
                    pass #?
                else:
                    qu.put((swapped, puz[0]))
     #some sort of linked list like function to get the path of result to start
     #here

EXAMPLE OF VISITED NODES (list of tuples)访问节点示例(元组列表)

[('12-453786', ''), 
 ('1-2453786', '12-453786'), 
 ('12345-786', '12-453786'), 
 ('-12453786', '1-2453786'), 
 ('12-453786', '1-2453786'), 
 ('1524-3786', '1-2453786'), 
 ('1234-5786', '12345-786'), 
 ('12345678-', '12345-786')]

The expected result for this particular puzzle should be 2 moves这个特定谜题的预期结果应该是 2 步

Using your current data structure (array of (nextState, prevState) tuples) you can definitely work your way back to the start state, but it would not be very efficient since you have to scan the whole list every time to find the state you're looking for.使用您当前的数据结构((nextState,prevState)元组数组),您绝对可以回到开始 state 的方式,但这不会非常有效,因为您每次都必须扫描整个列表才能找到 state重新寻找。 Instead you could use a simple data structure for storing your BFS graph.相反,您可以使用简单的数据结构来存储 BFS 图。 That way when you reach the end state, you can just follow the back links to the initial state.这样,当您到达末尾 state 时,您只需按照后面的链接指向初始 state。

Also in your current code, you're not guarding against states you have visited before, so you could get into infinite loop situations and your while loop will never break.同样在您当前的代码中,您没有防范您之前访问过的状态,因此您可能会陷入无限循环的情况,而您的 while 循环将永远不会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM