我找到了密切相关的主题,但它没有回答我的问题。我的single_insert_or_delete
函数会返回0
,1
或2
,并根据该值我想在我的spelling_corrector
函数中使用它,但到目前为止,我是遇到麻烦,当我使用第一个函数返回的if value:
作为我的主python函数的参数时,它不会接受这些值而python返回None。我在这里想念什么?。
def spelling_corrector(s1,s2):
out_list=[]
def single_insert_or_delete(s1,s2):
a=s1.lower()
b=s2.lower()
if (len(a)==len(b))and (a==b):
return 0
elif len(a)>len(b):
if b in a:
if len(a)-len(b)==1:
return 1
else:
return 2
elif len(a)< len(b):
if a in b:
if len(b)-len(a)==1:
return 1
else:
count =0
for x in a:
if x in b:
count+=1
if len(b)-count==1:
return 1
else:
return 2
else:
return 2
这是主程序
s1= s1.split() #This will return list
for x in s1:
single_insert_or_delete(x,s2)
if 0:
out_list.append(x)
elif 1:
out_list.append(x)
elif 2:
out_list.append(x)
else:
out_list.append(x)
out_str=" ".join(out_list)
return out_str
测试输出
a="That is the Firs cas"
b=['that','first','case','car']
x=spelling_corrector(a,b)
print(x)
答案 0 :(得分:0)
在Python中没有神奇的返回值用法,你必须明确地使用它:
s1= s1.split() #This will return list
for x in s1:
insert_or_delete = single_insert_or_delete(x,s2)
if insert_or_delete == 0:
out_list.append(x)
elif insert_or_delete == 1:
out_list.append(x)
elif insert_or_delete == 2:
out_list.append(x)
else:
out_list.append(x)
out_str=" ".join(out_list)
return out_str