如果第一个答案不正确,则difflib.get_close_matches会在列表中抛出名称 [英] difflib.get_close_matches throw out names in a list if first answer isn't correct
问题描述
这是我上一个问题的更新版本,此处 .我在代码中添加了以下内容:如果get_close_matches名称不是他们想要的人的名字,则丢弃最接近的匹配项,然后重新运行该函数并获取第二个最接近的匹配项(现在是第一个,因为该函数将抛出淘汰第一场比赛.
Here's an updated version from my previous question here. I'm adding to the code where if the get_close_matches name isn't the name of the person they wanted, then discard the closest match and re-run the function and grab the second-closest match (now first, since the function would throw out the first match).
您对此有何更好的评论?和工作. >.>
Do you have any comments on how this can be written better? And work. >.>
这是我到目前为止所拥有的:
Here's what I have so far:
def throwout(pickedName):
employeeNames.remove(pickedName)
pickedName = difflib.get_close_matches(userEmpName, employeeNames, 1)
print(pickedName)
userNameOK = input("Is this the name of the person you're looking for?\n\n Type 'Y' or 'N'.\n\n")
employeeNames = ['Colton','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
userEmpName = input("Please enter the employee name you're searching for. We'll return the best match on record.")
pickedName = difflib.get_close_matches(userEmpName, employeeNames, 1)
print(pickedName)
userNameOK = input("Is this the name of the person you're looking for?\n\n Type 'Y' or 'N'.\n\n")
if userNameOK == "N" or "n":
if pickedName in employeeNames:
throwout(pickedName)
else:
break
else:
break
列表中的名称用尽错误:
Error for running out of names in the list:
Traceback (most recent call last):
File "C:/Python33/employee1.py", line 64, in <module>
print(userAnswer + " is the right choice.\n")
NameError: global name 'userAnswer' is not defined
我理解这意味着由于名称列表不再具有将其全部删除的名称,因此全局变量"userAnswer"是未定义的.
I understand that to mean that since the list of names has no more names from removing them all, the global variable "userAnswer" is then undefined.
推荐答案
由于list.remove(name)
在同一行中执行相同的操作,因此无需创建将名称从列表中排除的函数.
There is no need of creating a function to throw out the names from list as list.remove(name)
does the same thing in a single line.
import difflib
employeeNames = ['Colton','Coltron','Colty','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
userEmpName = raw_input("Please enter the employee name you're searching for. We'll return the best match on record.")
while True:
global Answer
pickedName = difflib.get_close_matches(userEmpName, employeeNames, 1)
print(pickedName)
print employeeNames
if len(pickedName)==0:
break
userNameOK = raw_input("Is this the name of the person you're looking for?\n\n Type 'Y' or 'N'.\n\n")
if (userNameOK=='n' or userNameOK=='N'):
employeeNames.remove(pickedName[0])
else:
Answer=pickedName[0]
break
print Answer+" is the right choice"
但是,使用全局变量通常是不好的做法,因此您可以使函数执行所有操作并返回正确的Answer
However using a global variable is generally bad practice so you can make a function to do all this thing and return the correct Answer
此外,每次从名称中删除employeeNames
时都会对其进行修改,因此最好创建列表的副本并在该特定列表上工作
Also as employeeNames
is modified every time a Name is removed from it should be better to create a copy of the list and work on that particular list
这篇关于如果第一个答案不正确,则difflib.get_close_matches会在列表中抛出名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!