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

时间:2018-12-06 01:23:26

标签: python python-3.x dictionary

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

如何拆分输入字符串以对照字典“ 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")

1 个答案:

答案 0 :(得分:3)

假设输入是一个单词列表,并用空格隔开:

REMOVE CategoryProductRelation;target[unique=true](code,$catalogVersion);source[unique=true](code,$catalogVersion)

;xyz;cat1,cat2,cat3

输出

const makeDimensionsEqual = (someTextareaSelector, someParentElementSelector) => {

  document.querySelectorAll(someTextareaSelector)[0].style.height = 
    document.querySelectorAll(someParentElementSelector)[0]
    .getBoundingClientRect().height + "px"

}

const someTextareaSelector = '...' 
const someParentElementSelector = '...'
makeDimensionsEqual(someTextareaSelector, someParentElementSelector)

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 遍历短语中的单词。

进一步

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