你好,我正在尝试输入一个字符串,然后将该字符串拆分为单个单词。在字符串中独特的单词也是“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")