将字符串中的单词与字典键进行匹配

3

你好,我正在尝试输入一个字符串,然后将该字符串拆分为单个单词。在字符串中独特的单词也是“contents”字典键中的单词,从字典“files”检索相应的值。

如何拆分输入字符串以检查单个单词是否与字典“concept”键匹配,并如果可能返回字符串中的单词而不是字典键?

我尝试将字符串拆分为列表,然后直接将列表值传递到字典中,但我很快就迷失了方向(这些是在顶部注释掉的变量)。感谢任何帮助。谢谢。

def concept(word):

# convert var(word) to list
#my_string_list=[str(i) for i in word]

# join list(my_string_list) back to string
#mystring = ''.join(my_string_list)

# use this to list python files
files    = {1:"file0001.txt",
            2:"file0002.txt",
            3:"file0003.txt",
            4:"file0004.txt",
            5:"file0005.txt",
            6:"file0006.txt",
            7:"file0007.txt",    
            8:"file0008.txt",
            9:"file0009.txt"}

# change keys to searchable simple keyword phrases. 
concepts = {'GAMES':[1,2,4,3,3],
            'BLACKJACK':[5,3,5,3,5],
            'MACHINE':[4,9,9,9,4],
            'DATABASE':[5,3,3,3,5],
            'LEARNING':[4,9,4,9,4]}

# convert to uppercase, search var(mystring) in dict 'concepts', if not found return not found"
if word.upper() not in concepts:
    print("{}: Not Found in Database" .format(word)) not in concepts
    return

# for matching keys in dict 'concept' list values in dict 'files'
for pattern in concepts[word.upper()]:
    print(files[pattern])


# return input box at end of query        
while True:
    concept(input("Enter Concept Idea: "))
    print("\n")

你好,欢迎来到SO。你可以尝试这样做:对于字典中的word.upper(),使用dictionary.keys()进行匹配,如果匹配成功,则输出'matched!!'。 - Arka Mallick
1个回答

3
假设输入是由空格分隔的单词列表,您可以这样做:
def concept(phrase):

    words = phrase.split()

    # use this to list python files
    files = {1: "file0001.txt",
             2: "file0002.txt",
             3: "file0003.txt",
             4: "file0004.txt",
             5: "file0005.txt",
             6: "file0006.txt",
             7: "file0007.txt",
             8: "file0008.txt",
             9: "file0009.txt"}

    # change keys to searchable simple keyword phrases.
    concepts = {'GAMES': [1, 2, 4, 3, 3],
                'BLACKJACK': [5, 3, 5, 3, 5],
                'MACHINE': [4, 9, 9, 9, 4],
                'DATABASE': [5, 3, 3, 3, 5],
                'LEARNING': [4, 9, 4, 9, 4]}

    for word in words:
        # convert to uppercase, search var(mystring) in dict 'concepts', if not found return not found"
        if word.upper() not in concepts:
            print("{}: Not Found in Database".format(word))
        else:
            # for matching keys in dict 'concept' list values in dict 'files'
            for pattern in concepts[word.upper()]:
                print(files[pattern])

concept("games blackjack foo")

输出

file0001.txt
file0002.txt
file0004.txt
file0003.txt
file0003.txt
file0005.txt
file0003.txt
file0005.txt
file0003.txt
file0005.txt
foo: Not Found in Database

这行代码words = phrase.split()会将字符串phrase按空格分割。要检查一个单词是否在字典中,你需要逐个检查每个单词,因此需要使用循环for word in words来遍历phrase中的单词。

更进一步

  1. 如何检查一个字典中是否存在某个键?
  2. 如何在Python中使用分隔符拆分字符串?

谢谢,非常接近了。有没有办法返回不在字典中的多个单词?例如,如果输入是“今天游戏是二十一点”,它能否返回“今天”和“是”:未在数据库中找到。我似乎无法使其迭代并检查所有不在字典中的单词。 - Agla
我刚刚将那个字符串传递给了函数概念,它打印出:今天:数据库中未找到 file0001.txt file0002.txt file0004.txt file0003.txt file0003.txt 是:数据库中未找到 file0005.txt file0003.txt file0005.txt file0003.txt file0005.txt - Dani Mesejo
以上输出包括了 Todayare - Dani Mesejo
抱歉,我在编辑时不小心把“return”语句留在了“If”条件下面。为什么会有影响? - Agla
是的,实际上这可能会导致只打印到概念中第一个“no”单词。 - Dani Mesejo
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接