sd=['word','dd','da','dc','dword','d'] # 计算出下一个接龙单词 def jl(sd,st):# sd.remove(st)sd = list(set(sd))sends = list(st)[-1]lg = []sd.sort()for i in sd:if i.startswith(sends):lg.append((i, len(i)))if lg==[]:return 0,0lg.sort(key=lambda x: x[1],reverse=True)maxlen = lg[0][1]maxlenl = []for i in lg:if maxlen <= i[1]:maxlenl.append(i)if len(maxlenl) != 1:maxlenl.sort(key=lambda k: k[0])sd.remove(maxlenl[0][0])return maxlenl[0][0],sdres=[] res.append(sd[4]) ld=len(sd) st=sd[4] sd.remove(sd[4]) for i in range(0,ld):st,sd=jl(sd,st)if st:res.append(st)else:break print(res)