关于python:将字符串中的单个单词与字典键匹配

Match individual words in a string to dictionary keys

嗨,我正在尝试输入一个字符串,然后将该字符串拆分为单个单词。字符串中以及"contents"的字典键中的唯一单词从字典"files"中检索相应的值。

如何拆分输入字符串以对照字典"concept"键检查单个单词,如果可能,返回字符串中的单词,而不是字典键?

我试图将字符串拆分成一个列表,然后将列表值直接传递到字典中,但我很快就丢失了(这些是在顶部注释掉的变量)。感谢您的帮助。谢谢你

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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("
"
)


假设输入是由空格分隔的单词列表,则可以执行以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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")

产量

1
2
3
4
5
6
7
8
9
10
11
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迭代短语中的单词。

进一步

  • 如何检查字典中是否存在密钥?
  • 用python中的分隔符拆分字符串