我一直在研究此代码,并且到目前为止已经尝试调试了将近一天,但是我看不到问题出在哪里。

我在第66行停止了调试器。
当我进入或进入代码时,会收到错误消息。

Traceback (most recent call last):
  File "/home/johan/pycharm-community-4.5.3/helpers/pydev/pydevd.py", line 2358, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/johan/pycharm-community-4.5.3/helpers/pydev/pydevd.py", line 1778, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/johan/sdp/lets_learn_python/20_a_star_algorithm.py", line 87, in <module>
    a.solve()
  File "/home/johan/sdp/lets_learn_python/20_a_star_algorithm.py", line 66, in solve
    closest_child.create_children()
  File "/home/johan/sdp/lets_learn_python/20_a_star_algorithm.py", line 48, in create_children
    child = StateString(val, self)
  File "/home/johan/sdp/lets_learn_python/20_a_star_algorithm.py", line 32, in __init__
    self.dist = self.get_dist()
  File "/home/johan/sdp/lets_learn_python/20_a_star_algorithm.py", line 40, in get_dist
    dist += abs(i - self.value.index(letter))
ValueError: substring not found


这是我一直在努力的代码。
这来自Trevor Payne的教程。

#!usr/bin/env python

from Queue import PriorityQueue


class State(object):
    def __init__(self, value, parent, start=0, goal=0):
        self.children = []
        self.parent = parent
        self.value = value
        self.dist = 0
        if parent:
            self.path = parent.path[:]
            self.path.append(value)
            self.start = parent.start
            self.goal = parent.goal
        else:
            self.path = [value]
            self.start = start
            self.goal = goal

    def get_dist(self):
        pass

    def create_children(self):
        pass


class StateString(State):
    def __init__(self, value, parent, start=0, goal=0):
        super(StateString, self).__init__(value, parent, start, goal)
        self.dist = self.get_dist()

    def get_dist(self):
        if self.value == self.goal:
            return 0
        dist = 0
        for i in range(len(self.goal)):
            letter = self.goal[i]
            dist += abs(i - self.value.index(letter))
        return dist

    def create_children(self):
        if not self.children:
            for i in xrange(len(self.goal)-1):
                val = self.value
                val = val[:i] + val[i+1] + val[i] + val[i+2]
                child = StateString(val, self)
                self.children.append(child)


class AStarSolver:
    def __init__(self,start, goal):
        self.path = []
        self.visited_queue = []
        self.priority_queue = PriorityQueue()
        self.start = start
        self.goal = goal

    def solve(self):
        start_state = StateString(self.start, 0, self.start, self.goal)
        count = 0
        self.priority_queue.put((0, count, start_state))
        while not self.path and self.priority_queue.qsize():
            closest_child = self.priority_queue.get()[2]
            closest_child.create_children()
            self.visited_queue.append(closest_child.value)
            for child in closest_child.children:
                if child.value not in self.visited_queue:
                    count += 1
                    if not child.dist:
                        self.path = child.path
                        break
                    self.priority_queue.put(child.dist, count)
        if not self.path:
            print "Goal of {0} is not possible!".format(self.goal)
        return self.path

# ================================
# MAIN

if __name__ == "__main__":
    start1 = "acbda"
    goal1 = "dabcd"
    print "Starting..."
    a = AStarSolver(start1, goal1)
    a.solve()
    for i in xrange(len(a.path)):
        print "%d) " % i + a.path[i]


我真的希望有人能帮助我解决这个问题。

最佳答案

letterself.value中不存在时,您应该进行处理(第41行):

dist += abs(i - self.value.index(letter))


当字母不存在时,string.index会引发异常。最好使用string.find代替,如果找不到字母,则会给出-1

dist += abs(i - self.value.find(letter))


如果要提供-1以外的其他值,则应在使用前对其进行测试:

f = self.value.find(letter)
if f == -1:
   # DO what you want

关于python - Python ValueError:找不到子字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32035731/

10-12 07:25