difflib.get_close_matches()-帮助获得所需的结果 [英] difflib.get_close_matches() - Help getting desired result

查看:493
本文介绍了difflib.get_close_matches()-帮助获得所需的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序的基本要点是从雇员姓名列表开始,然后对其进行排序.等待用户输入"end"以停止填充名称列表(我有100个名称,在此示例中简称为).然后,用户可以输入员工名称,程序将运行difflib.get_close_matches().

The basic gist of the program is to start with a list of employee names, then sort it. Wait for user to input "end" to stop populating the list of names (I have 100 names, I cut it short for the example). Afterwards, the user can enter an employee name and the program will run difflib.get_close_matches().

这是问题;我收到get_close_matches的语法错误.我应该如何以不同的方式输入difflib?还;如果您有任何使代码更高效的技巧,请说明如何以及为什么更高效.我对Python缺乏经验,所以请保持谦虚,是吧?

Here's the question; I'm getting a syntax error for get_close_matches. How should I be entering the difflib differently? Also; if you have any tips for making the code more efficient, please also state how and why it's more efficient. I'm fairly inexperienced with Python, so be gentle, eh?

示例代码:

import difflib
employeeNames = ['Colton','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
endInput = input('Type "end" to view list of names.\n\n')
if endInput == "end":
    userEmpName = input("Please enter the employee name you're searching for. We'll return the best match on record."
get_close_matches(userEmpName, employeeNames, 1)

推荐答案

您的代码具有语法错误: 将此代码与您的代码匹配:

Your code has syntax errors: Match this code with yours:

import difflib
employeeNames = ['Colton','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
endInput = raw_input('Type "end" to view list of names.\n\n')
if endInput == "end":
    userEmpName = raw_input("Please enter the employee name you're searching for. We'll return the best match on record.")
    print difflib.get_close_matches(userEmpName, employeeNames, 1)

  1. 您没有在input()方法中合上打开的花括号.

  1. you didn't close the open brace in input() method.

我建议在处理字符串时使用raw_input()而不是使用input().

I suggest using raw_input() instead of using input() while dealing with strings.

如果仅导入了类(在您的情况下为import difflib),则应使用classname.method(),因此应使用difflib.get_close_matches(string,list,n).

you should use the classname.method() if you have imported only the class (in your case import difflib) so use difflib.get_close_matches(string,list,n) instead.

您需要在返回值之前使用print语句.

You need to use print statement before the returned value.

还应该在if内部调用get_close_matches(),因为如果endInput!='end',则NameError会出现在userEmpName上.

Also get_close_matches() should be called inside of if because if endInput!='end' then NameError will occur for userEmpName.


我应该问过您有关python解释器版本的信息.
打印行应使用这样的花括号.

I should have asked you about your python interpreter version.
The print line should use braces like this.


print(difflib.get_close_matches(userEmpName, employeeNames, 1))

原因是在python 2.x中打印的是statement(正如我在第4点提到的那样),但是在python 3.x中的打印的原因是function.

The reason is in python 2.x print is a statement(as I mentioned in 4rth point) but in python 3.x its a function.

这篇关于difflib.get_close_matches()-帮助获得所需的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆