【问题标题】:Match individual words in a string to dictionary keys将字符串中的单个单词与字典键匹配
【发布时间】:2018-12-06 01:23:25
【问题描述】:

您好,我正在尝试输入一个字符串,然后将该字符串拆分为单个单词。字符串中的唯一词以及“内容”的字典键中的唯一词从字典“文件”中检索相应的值。

如何拆分输入字符串以根据字典“概念”键检查单个单词,并在可能的情况下返回字符串中的单词,而不是字典键?

我尝试将字符串拆分为一个列表,然后将列表值直接传递到字典中,但我很快就迷路了(那些是在顶部注释掉的变量。感谢您的帮助。谢谢

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。你这样做怎么样,对于 dictionary.keys() 中的 word.upper(): print('matched!!')

标签: python python-3.x dictionary


【解决方案1】:

假设输入是由空格分隔的单词列表,您可以这样做:

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() 行将字符串短语拆分为空格。要检查一个单词是否在字典中,您需要一次执行一个,因此循环 for word in words 会遍历短语中的单词。

进一步

  1. How can I check if a key exists in a dictionary?
  2. Split a string by a delimiter in python

【讨论】:

  • 哇,你的速度比我快得多,而且你的回答也好得多:) 只有一条评论,但如果 word.upper() 不在 concept.keys() 中,可能不适用: 更适合你认为的需求?
  • 我相信大多数 pythonic 只是为了如果 word.upper() 不在概念中。
  • 感谢您的澄清。你是对的,我的印象是错误的。 +1
  • 谢谢你,非常非常接近。有没有办法返回字典中未列出的多个单词。例如,如果输入是“今天的游戏是二十一点”。能否返回“今天”和“现在”:在数据库中未找到。我似乎无法让它迭代并检查字典中没有的所有单词。
  • 我刚刚将该字符串传递给函数概念并打印:Today: Not Found in Database file0001.txt file0002.txt file0004.txt file0003.txt file0003.txt are: Not Found in Database file0005.txt file0003.txt file0005.txt file0003.txt file0005.txt
猜你喜欢
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多