我有一本字典,其中值对应于单词,其键对应于这些单词的类别。我想检查句子中是否存在这些单词/值,如果是,则返回类别/关键字,否则返回“其他”作为类别。
由于类别众多,因此代码应能够检查循环中每个句子的键值。
我写了一些东西,但是没有输出。我知道这是不正确的,但是我被困在这里。
for i in data:
if dictio.values() in i:
print (dictio.keys())
在上面的代码中,我只是打印类别,但是我想要一个函数,该函数将为句子中匹配的值返回键。
我的数据是:
data = ["my web portal is not working","online is better than offline", "i like going to pharmacy shop for medicines",
"web is the future", "i love apple"]
我的字典是:
dictio = {'fruits':['apple'], 'web': ['web', 'online']}
因此代码应检查句子中的(web / online / apple)并返回键/类别作为输出,即(Web / Fruits)
通过此代码,我得到输出:['web','web','web','fruits']
matches[]
for string in data:
for category, words in dictio.items():
for word in words:
if word in string:
matches.append(category)
break
print(matches)
答案 0 :(得分:1)
data = ["my web portal is not working","online is better than offline", "i like going to pharmacy shop for medicines", "web is the future", "i love apple"]
dictio = {'fruits':['apple'], 'web': ['web', 'online']}
for string in data: # iterate over each string
matches = [] # Create a list to store the matches for this string
for category, words in dictio.items(): #iterate over the keys/values to check each
for word in words: # For each word
if word in string: # Check if it appears in the string
matches.append(category) #If it does then add the category to the matches
break # Break to only add each category once at most
print(matches) # print results
输出:
['web']
['web']
[]
['web']
['fruits']
更加简洁和“ pythonic”
for string in data: # iterate over each string
matches = [] # Create a list to store the matches for this string
for category, words in dictio.items(): #iterate over the keys/values to check each
matches += set([category for word in words if word in string]) #Set will remove duplicates
print(matches) # print results
答案 1 :(得分:0)
尝试一下:
data = [
"my web portal is not working",
"online is better than offline",
"i like going to pharmacy shop for medicines",
"web is the future",
"i love apple"
]
dictio = {
'fruits': ['apple'],
'web': [
'web',
'online'
]
}
matches = []
status = True
for string in data:
status = True
for key, values in dictio.items():
for value in values:
if value in string:
matches.append(key)
status = False
break
if status:
matches.append('other')
print(matches)
输出:
['web', 'web', 'other', 'web', 'fruits']