Python-在字典中搜索字符串
基本上,我有一个故障排除程序,我希望用户输入他们的输入。然后,我接受这个输入并将单词分割成单独的字符串。之后,我想从.CSV文件的内容创建一个字典,其中键作为可识别的关键字,第二列作为解决方案。最后,我想检查split users输入的任何字符串是否在dictionary键中,打印解决方案 然而,我面临的问题是,我可以做我上面所说的,但是,它循环通过,如果我的输入是“我的手机是湿的”,“湿的”是一个可识别的关键字,它将通过并说“未识别”,“未识别”,“未识别”,然后最后它将打印解决方案。因为字符串“My”、“phone”和“is”无法识别,所以它多次表示无法识别 那么,我如何测试用户拆分的输入是否在我的字典中,而不输出“未识别”等 对不起,如果不清楚的话,我对整件事很困惑 代码: 注意,我有通行证,因为这是我需要帮助的部分 我的CSV文件包括: 问题关键字|解决方案 比如,Python-在字典中搜索字符串,python,csv,dictionary,Python,Csv,Dictionary,基本上,我有一个故障排除程序,我希望用户输入他们的输入。然后,我接受这个输入并将单词分割成单独的字符串。之后,我想从.CSV文件的内容创建一个字典,其中键作为可识别的关键字,第二列作为解决方案。最后,我想检查split users输入的任何字符串是否在dictionary键中,打印解决方案 然而,我面临的问题是,我可以做我上面所说的,但是,它循环通过,如果我的输入是“我的手机是湿的”,“湿的”是一个可识别的关键字,它将通过并说“未识别”,“未识别”,“未识别”,然后最后它将打印解决方案。因为字符
wet把电话放在一碗米饭里。在循环后加上一个布尔值和一个if,只有当句子中的单词都不被识别时才会运行。我想你可能可以使用类似于:
for word in Problem:
if KeywordsCSV.has_key(word):
KeywordsCSV.get(word)
while True:
problem = eg.enterbox(...) # as above
solution = problems.get(problem)
if solution is None:
# invalid problem, warn the user
else:
# display the solution? Do whatever it is you're doing with it and...
break
或列表:
[KeywordsCSV.get(word) for word in Problem if KeywordsCSV.has_key(word)]
你的代码读起来像一些丑陋的代码。在我们研究如何解决这个问题之前,让我们先把它整理一下
import easygui as eg
import csv
# # KeywordsCSV = dict(csv.reader(open('Keywords and Solutions.csv')))
# why are you nesting THREE function calls? That's awful. Don't do that.
# KeywordsCSV should be named something different, too. `problems` is probably fine.
with open("Keywords and Solutions.csv") as f:
reader = csv.reader(f)
problems = dict(reader)
problem = eg.enterbox('Please enter your problem: ', 'Troubleshooting').lower().split()
# this one's not bad, but I lowercased your `Problem` because capital-case
# words are idiomatically class names. Chaining this many functions together isn't
# ideal, but for this one-shot case it's not awful.
让我们在这里休息一下,注意到我在你的每一行代码上都做了一些修改。尽可能花时间熟悉PEP8!它将极大地改进用Python编写的任何代码
无论如何,一旦你有了一个问题
dict,并且一个问题
应该是该dict中的一个键,你可以做:
if problem in problems:
solution = problems[problem]
甚至使用默认返回的dict.get
:
solution = problems.get(problem)
# if KeyError: solution is None
如果要循环此操作,可以执行以下操作:
for word in Problem:
if KeywordsCSV.has_key(word):
KeywordsCSV.get(word)
while True:
problem = eg.enterbox(...) # as above
solution = problems.get(problem)
if solution is None:
# invalid problem, warn the user
else:
# display the solution? Do whatever it is you're doing with it and...
break
请发布您的代码:)并使用包含的代码进行编辑。您没有包含代码中断的部分。不要因为你知道你的代码在那里不起作用而仅仅通过
测试,让我们看看你已经尝试了什么为什么在关键字csv中使用if Problem:…
来测试它是否在dict中?dict.has\u key
不在Python3中,也不应该在Python2中使用。在dict中使用键
而不是dict.has_key(key)
但是,如果您正在检查它是否有键,为什么要使用dict.get
?只需使用item accessKeywordsCSV[word]
就可以了。我不知道about有_key
。我一直认为,如果该方法是内置的,那么使用起来很好。对于dict.get()
,我认为内置方法比使用括号snope更好。dict.get有一些很好的用途,但是如果你知道有一个键,你应该使用item access。好的,在阅读了这篇文章并使用你说我可以使用的while循环建议对它进行测试之后,我得到了一个错误:TypeError:“dict”对象是不可调用的。经过一点研究,如果你明白我的意思,我就是找不到与我的代码类型相关的解决方案。我没有调用任何dicts,因此你一定没有正确复制我的代码。好的,再次检查后,我删除了我的所有代码,并从这里完全复制了它,并得到以下错误:/solution=problems.get(problem)TypeError:unhabable type:'list'@Thom9son啊,是的,您得到一个列表是因为您正在调用str.split
。我不知道easygui.enterbox返回什么。您的意思是执行(…).lower().strip()
?拆分用户输入会提供与拆分相同的结果吗?因为我想分割它,这样我就可以搜索关键字,就像你帮我做的那样。