如何计算字符串中的某些单词(不仅仅是一个单词),然后如果单词数量不同则输出不同的代码?
How do I count up certain words (not just one word) in a string and then output different code if there are different amounts of words?
抱歉,这个问题很长,我不知道如何在不将其标记为重复的情况下具体措辞。无论如何,我正在尝试计算用户输入的一组关键字,这些关键字被写入文本文件。这是代码:
import os
def keyword_searcher():
user_query = input("Enter your problem:\n")
with open("user_query.txt", "a+") as query:
query.write(user_query.lower())
query.seek(0)
for line in query:
for word in line.split():
if word in ("backlight", "display", "cracked", "touchscreen"):
with open("Solutions1.txt", "r") as f:
solution_one = f.read()
print ("Keyword for screen problem found:\n")
print (solution_one)
elif word in ("battery", "charger", "usb", "charge"):
with open("Solutions2.txt", "r") as f_two:
solution_two = f_two.read()
print ("Keyword for battery problem found:\n")
print(solution_two)
elif word in ("virus", "hacked", "infected", "antivirus"):
with open("Solutions3.txt", "r") as f_three:
solution_three = f_three.read()
print ("Keyword for virus problem found:\n")
print (solution_three)
else:
pass
keyword_searcher()
os.remove("user_query.txt")
我的问题是,当用户输入多个关键字时,它会输出那么多次代码,而我只需要一次。我该如何解决这个问题?
for word in line.split():
if word in ("backlight", "display", "cracked", "touchscreen"):
...
意味着您将在循环的每次迭代中检查一个词,因此如果有多个词匹配,您将得到重复项。要仅在列表中存在至少一个单词时执行此操作,请使用 set
s:
for line in query:
words = set(line.split())
if words & {"backlight", "display", "cracked", "touchscreen"}:
...
或者,您可以执行类似
的操作
for line in query:
words = line.split()
if any(word in ("backlight", "display", "cracked", "touchscreen") for word in words):
...
这可能更容易阅读,但会更慢。
你真的可以精简这个程序。不需要将用户输入写入文件并在最后删除它,只需将其存储为变量即可。您应该使用 if
语句检查每个语句,以确保它检查每个语句。此外, else: pass
是多余的。希望这对你有所帮助,祝你的程序好运:
list_one = ["backlight", "display", "cracked", "touchscreen"]
list_two = ["battery", "charger", "usb", "charge"]
list_three = ["virus", "hacked", "infected", "antivirus"]
user_query = input("Enter your problem:\n").split()
if any(word in list_one for word in user_query):
with open("Solutions1.txt") as f:
solution_one = f.read()
print("Keyword for screen problem found:\n")
print(solution_one)
if any(word in list_two for word in user_query):
with open("Solutions2.txt") as f_two:
solution_two = f_two.read()
print("Keyword for battery problem found:\n")
print(solution_two)
if any(word in list_three for word in user_query):
with open("Solutions3.txt") as f_three:
solution_three = f_three.read()
print("Keyword for virus problem found:\n")
print(solution_three)
抱歉,这个问题很长,我不知道如何在不将其标记为重复的情况下具体措辞。无论如何,我正在尝试计算用户输入的一组关键字,这些关键字被写入文本文件。这是代码:
import os
def keyword_searcher():
user_query = input("Enter your problem:\n")
with open("user_query.txt", "a+") as query:
query.write(user_query.lower())
query.seek(0)
for line in query:
for word in line.split():
if word in ("backlight", "display", "cracked", "touchscreen"):
with open("Solutions1.txt", "r") as f:
solution_one = f.read()
print ("Keyword for screen problem found:\n")
print (solution_one)
elif word in ("battery", "charger", "usb", "charge"):
with open("Solutions2.txt", "r") as f_two:
solution_two = f_two.read()
print ("Keyword for battery problem found:\n")
print(solution_two)
elif word in ("virus", "hacked", "infected", "antivirus"):
with open("Solutions3.txt", "r") as f_three:
solution_three = f_three.read()
print ("Keyword for virus problem found:\n")
print (solution_three)
else:
pass
keyword_searcher()
os.remove("user_query.txt")
我的问题是,当用户输入多个关键字时,它会输出那么多次代码,而我只需要一次。我该如何解决这个问题?
for word in line.split():
if word in ("backlight", "display", "cracked", "touchscreen"):
...
意味着您将在循环的每次迭代中检查一个词,因此如果有多个词匹配,您将得到重复项。要仅在列表中存在至少一个单词时执行此操作,请使用 set
s:
for line in query:
words = set(line.split())
if words & {"backlight", "display", "cracked", "touchscreen"}:
...
或者,您可以执行类似
的操作for line in query:
words = line.split()
if any(word in ("backlight", "display", "cracked", "touchscreen") for word in words):
...
这可能更容易阅读,但会更慢。
你真的可以精简这个程序。不需要将用户输入写入文件并在最后删除它,只需将其存储为变量即可。您应该使用 if
语句检查每个语句,以确保它检查每个语句。此外, else: pass
是多余的。希望这对你有所帮助,祝你的程序好运:
list_one = ["backlight", "display", "cracked", "touchscreen"]
list_two = ["battery", "charger", "usb", "charge"]
list_three = ["virus", "hacked", "infected", "antivirus"]
user_query = input("Enter your problem:\n").split()
if any(word in list_one for word in user_query):
with open("Solutions1.txt") as f:
solution_one = f.read()
print("Keyword for screen problem found:\n")
print(solution_one)
if any(word in list_two for word in user_query):
with open("Solutions2.txt") as f_two:
solution_two = f_two.read()
print("Keyword for battery problem found:\n")
print(solution_two)
if any(word in list_three for word in user_query):
with open("Solutions3.txt") as f_three:
solution_three = f_three.read()
print("Keyword for virus problem found:\n")
print(solution_three)