在列表中查找一个值,如果有多个值则打印所有值,否则打印另一个列表中的相应值
我有两个列表:
Keyword = ['Dog', 'Cat', 'White Cat', 'Lion', 'Black Cat']
Definition = ['Mans Best Friend', 'The cat is a domestic species of a small carnivorous mammal', 'White cats are cute', 'Lions are Carnivores Wild Animal', 'Black Cats are Black in color']
我从以下命令中获取“查询”中的语音输入:
import speech_recognition as sr
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language ='en-in')
print(f"User said: {query}\n")
except Exception as e:
print(e)
print("Unable to Recognize your voice.")
return "None"
return query
query = takeCommand().Capitalize()
现在,如果查询中包含狗,我想从列表中打印相应的定义,即“男人最好的朋友”,如果查询包含猫,我想向用户显示有多个关键字中包含“猫”,即“猫”、“白猫”、“黑猫”,如果查询中的单词不在我想要打印的列表中“没有关键字已找到,请检查您的关键字”
有人知道如何解决这个问题吗?
不同情况的输入输出:
输入:查询中有“Dog”。程序应检查是否有超过 1 个单词包含 Dog。如果是,则应打印所有包含 Dog 的关键字,如果否,则应打印相应的定义。在这种情况下,关键字的输出应该是相应的定义,即“Mans Best Friend”。
输入:查询中有“Cat”。在这个关键字例子中,有 3 个关键字包含 cat,即 'Cat' 、 'Black Cat' 、 'White Cat' 所以这里的代码应该打印这些关键字而不是它们的定义。所以这个案例的输出:我们找到了多个关键字:'Cat','Black Cat','White Cat'
输入:查询中有'Panther'。关键字中没有 Panther,因此应该打印“没有匹配的关键字”。
I have two lists :
Keyword = ['Dog', 'Cat', 'White Cat', 'Lion', 'Black Cat']
Definition = ['Mans Best Friend', 'The cat is a domestic species of a small carnivorous mammal', 'White cats are cute', 'Lions are Carnivores Wild Animal', 'Black Cats are Black in color']
I am getting a voice input in 'query' from the following command:
import speech_recognition as sr
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language ='en-in')
print(f"User said: {query}\n")
except Exception as e:
print(e)
print("Unable to Recognize your voice.")
return "None"
return query
query = takeCommand().Capitalize()
Now if the query contains Dog in it I want to print the corresponding definition from the list i.e. 'Man's Best Friend', If the query contains Cat , I want to show the user that there are multiple Keywords that have 'Cat' in them i.e. 'Cat', 'White Cat', 'Black Cat' and if the word inside query isn't in the list I want to print "No keywords Found, Please check your Keyword"
Does anyone has an idea on how to solve this?
Input output for different cases:
Input: query has 'Dog' in it. The program should check for if there are more than 1 word that has Dog in it. If Yes it should print all the keywords that has Dog in it, if No then it should print the corresponding definition . In this case of Keywords the output for Dog should be the corresponding definition i.e. 'Mans Best Friend'.
Input: Query has 'Cat' in it. In this case of keywords there are 3 keywords that have cat in them i.e. 'Cat' , 'Black Cat', 'White Cat' So here the code should Print these Keywords instead of their definition. So output of this case : We have found multiple keywords : 'Cat' , 'Black Cat', 'White Cat'
Input : Query has 'Panther' in it. There is no Panther in Keywords So it should print "There are no Matching Keywords".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码的作用是:
Keyword
中输入的值是否存在,如果不存在则返回“There are no Matching keywords”。multiple_vals
。如果multiple_val
的长度大于1,则只会显示f“我们发现了多个关键字:{i_removed}”
。Definition
中相应的索引。What this code does is:
Keyword
or not, if not then would return "There are no Matching Keywords".multiple_vals
. If length ofmultiple_val
if greater than 1 then only it would showf"We have found multiple keywords : {i_removed}"
.Definition
.