在列表中查找一个值,如果有多个值则打印所有值,否则打印另一个列表中的相应值

发布于 2025-01-15 11:19:49 字数 1398 浏览 6 评论 0原文

我有两个列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-01-22 11:19:49
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']

def take_cmd(cmd):
    multiple_val=[]
    if cmd in Keyword:
        for i,j in enumerate(Keyword):
            if cmd in j:
                multiple_val.append((i,j))
        if len(multiple_val)>1:
            i_removed=[j for i in multiple_val for j in i if type(j)!=int]
            print(f"We have found multiple keywords : {i_removed}")
        else:
            print(Definition[Keyword.index(cmd)])
    else:
        print("There are no Matching Keywords")

这段代码的作用是:

  1. 检查Keyword中输入的值是否存在,如果不存在则返回“There are no Matching keywords”。
  2. 如果该值存在,则将检查是否存在多个实例或该值在多个索引中是否可用。
  3. 如果也返回 true,则将其附加到 multiple_vals。如果multiple_val的长度大于1,则只会显示f“我们发现了多个关键字:{i_removed}”
  4. 否则显示Definition中相应的索引。
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']

def take_cmd(cmd):
    multiple_val=[]
    if cmd in Keyword:
        for i,j in enumerate(Keyword):
            if cmd in j:
                multiple_val.append((i,j))
        if len(multiple_val)>1:
            i_removed=[j for i in multiple_val for j in i if type(j)!=int]
            print(f"We have found multiple keywords : {i_removed}")
        else:
            print(Definition[Keyword.index(cmd)])
    else:
        print("There are no Matching Keywords")

What this code does is:

  1. Checks if the value inputed exists in the Keyword or not, if not then would return "There are no Matching Keywords".
  2. If the value exists, then would check if there are multiple instances or the value is available in multiple indexes or not.
  3. If that returns true as well then it appends it to multiple_vals. If length of multiple_val if greater than 1 then only it would show f"We have found multiple keywords : {i_removed}".
  4. otherwise shows the corresponding index in Definition.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文