Python在字符串中搜索某些关键字

Python在字符串中搜索某些关键字,python,Python,现在我有一个每个单独关键字的列表,有没有一种方法可以将其放入一个数组或字符串中,并搜索单个单词,而不必将每个关键字分开并使其可重复 keywordlist = ("pricing") keywordlist1 = ("careers") keywordlist2 = ("quotes") keywordlist3 = ("telephone number") keywordlist5 = ("about") while True: question1 = raw_input("Is th

现在我有一个每个单独关键字的列表,有没有一种方法可以将其放入一个数组或字符串中,并搜索单个单词,而不必将每个关键字分开并使其可重复

keywordlist = ("pricing")
keywordlist1 = ("careers")
keywordlist2 = ("quotes")
keywordlist3 = ("telephone number")
keywordlist5 = ("about")
while True:
    question1 = raw_input("Is there anything I can help with today?   ")
    question_parts = question1.split(" ")
    for word in question_parts:
        if word.lower() in keywordlist:
            print("Yah i can show you some pricing:   ")
        elif word.lower() in keywordlist1:
            print("Here is our career page and feel free to apply:  ")
        elif word.lower() in keywordlist2:
            print("yah i can show you to our quote page: ")
        elif word.lower() in keywordlist3:
            print("Yah here is our contact page and feel free to contact us: ")
        elif word.lower() in keywordlist5:
            print("Here is some information about us:")
        goagain = raw_input("Would you like any More Help? (Yes/No)")
        if goagain == "Yes":
            #Get values again
            pass #or do whatever
        elif goagain != "Yes":
            print ("Bye!")
    break
你可以使用

你可以使用


使用正则表达式

import re 
KEYWORD_MAPPING = {
        "pricing": "Yah i can show you some pricing:",
        "careers": "Here is our career page and feel free to apply:",
        "quotes": "yah i can show you to our quote page:",
        "telephone": "Yah here is our contact page and feel free to contact us: ",
        "about": "Here is some information about us:"
}

def search_for_keyword(question):
    keyword_pattern = r'{}'.format('|'.join(KEYWORD_MAPPING.keys()))
    keyword = re.search(keyword_pattern, question, re.I).group()
    if not keyword:
         return False
    print(KEYWORD_MAPPING[keyword])
    return True

while True:
        question = input('enter a question')
        status = search_for_keyword(question)
        if not status:
             break

使用正则表达式

import re 
KEYWORD_MAPPING = {
        "pricing": "Yah i can show you some pricing:",
        "careers": "Here is our career page and feel free to apply:",
        "quotes": "yah i can show you to our quote page:",
        "telephone": "Yah here is our contact page and feel free to contact us: ",
        "about": "Here is some information about us:"
}

def search_for_keyword(question):
    keyword_pattern = r'{}'.format('|'.join(KEYWORD_MAPPING.keys()))
    keyword = re.search(keyword_pattern, question, re.I).group()
    if not keyword:
         return False
    print(KEYWORD_MAPPING[keyword])
    return True

while True:
        question = input('enter a question')
        status = search_for_keyword(question)
        if not status:
             break

你的
keywordlist
etc不是列表或元组,它们只是字符串。你的
keywordlist
etc不是列表或元组,它们只是字符串。干得好。FWIW,您可以将该
.split
调用更改为
question1.split()
,使其在所有空白处都被拆分。@PM2Ring Done!还可以将整个输入字符串转换为小写,以避免每次转换。非常感谢。好主意。我忘了提到这一点,尽管我在自己的回答中提到了(既然你已经发布了你的答案,我就不想麻烦发布了)。转换字符串的大小写非常便宜,但是在同一个字符串上反复转换没有意义。做得不错。FWIW,您可以将该
.split
调用更改为
question1.split()
,使其在所有空白处都被拆分。@PM2Ring Done!还可以将整个输入字符串转换为小写,以避免每次转换。非常感谢。好主意。我忘了提到这一点,尽管我在自己的回答中提到了(既然你已经发布了你的答案,我就不想麻烦发布了)。转换字符串的大小写非常便宜,但是在同一个字符串上反复转换没有意义。