简体   繁体   中英

Checking for dictionary key based on dictionary values in a sentence

I have a dictionary where values correspond to words and their keys correspond to categories for those words. I want to check whether these words/values exist in a sentence, if yes return category/key else return 'other' as a category.

As there are number of categories, the code should be capable of checking values for keys over each sentence in a loop.

I have written something, but not getting a output. I know this is not correct but I am stuck here.

for i in data:
    if dictio.values() in i:
        print (dictio.keys())

In the above code I am just printing the categories, but I want a function that will return key for values matched in sentence.

My data is:

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"]

my dictionary is:

dictio = {'fruits':['apple'], 'web': ['web', 'online']}

So the code should check (web/online/apple) in sentences and return key/category as output ie (Web/Fruits)

With this code I am getting output : ['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)
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

output:

['web']
['web']
[]
['web']
['fruits']

more concise and '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

Try this:

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)

Output:

['web', 'web', 'other', 'web', 'fruits']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM