Skip to main content
 首页 » 编程设计

Python 值错误 : Substring not found

2025年05月04日14fff_TT

我一直在研究这段代码,到目前为止我已经尝试调试了将近一天,但我无法找到问题所在。

我在第 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] 

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

请您参考如下方法:

当 self.value 中不存在 letter 时,您应该处理这种情况(第 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