【发布时间】:2011-11-22 05:29:32
【问题描述】:
我试图找出电影数据库中任意两个演员之间的分离度。 当我达到我的基本情况时,我成功了,即 1 度分离(即演员与另一个演员在同一部电影中)但我使用递归来找到所有其他分离度,我得到:
runtime error: maximum recursion depth exceeded in cmp.
##gets file with movie information
f = open("filename.txt")
actedWith = {}
ActorList = []
movies = {}
actedIn = []
dos = 1
def getDegrees(target, base, dos):
for actor in actedWith[base]:
if target == actor:
print base, "has ", dos, " degree(s) of separation from ", target
return
dos = dos+1
for actor in actedWith[base]:
getDegrees(target, actor, dos)
for l in f:
##strip of whitespace
l = l.strip()
##split by where forward-slashes are
l = l.split("/")
##add the first "word" on the line to the database of movie names
movies = {l[0] : l[1:]}
for e in l[1:]:
if e in actedWith:
actedWith[e] = actedWith[e]+movies[l[0]]
else:
actedWith[e] = movies[l[0]]
base = raw_input("Enter Actor Name (Last, First): ")
target = raw_input("Enter Second Actor Name (Last, First): ")
getDegrees(target, base, dos)
我使用的文本文件可以在http://www.mediafire.com/?qtryvkzmuv5jey3找到
为了测试基本情况,我使用:Bacon, Kevin 和 Pitt, Brad。
为了测试其他人,我使用 Bacon, Kevin 和 Gamble, Nathan。
【问题讨论】: