Skip to content Skip to sidebar Skip to footer

Matching Keywords In A Dictionary To A List In Python

The following dictionary gives the word and its value: keywords = {'alone': 1, 'amazed': 10, 'amazing': 10, 'bad': 1, 'best': 10, 'better': 7, 'excellent': 10, 'excited': 10, 'exci

Solution 1:

keywords = {'alone': 1, 'amazed': 10, 'amazing': 10, 'bad': 1, 'best': 10, 'better': 7, 'excellent': 10, 'excited': 10, 'excite': 10}
tweets = [['work', 'needs', 'to', 'fly', 'by', '', "i'm", 'so', 'excited', 'to', 'see', 'spy', 'kids', '4', 'with', 'then', 'love', 'of', 'my', 'life', '', 'arreic'], ['today', 'is', 'going', 'to', 'be', 'the', 'greatest', 'day', 'of', 'my', 'life', 'hired', 'to', 'take', 'pictures', 'at', 'my', 'best', "friend's", 'gparents', '50th', 'anniversary', '60', 'old', 'people', 'woo']]

values = []              # Here we will store the score of each tweat like an itemfor tweet in tweets:     # We iterate over each tweet
    values.append(0)     # We add a new item to the list values, we'll change this number later.for word in tweet:   # We iterate over each word in the tweetvalues[-1] += keywords.get(word, 0) # Using .get() we get the value of a word if it's inside keyword, if not, we get a default value: 0, instead of an KeyError. print(values) # Obviously, print the values in console

If you don't like the values.append(0) you can change it to new = 0 and the values[-1] to tmp. You will also need to add at the end of the first loop values.append(tmp). Also, remember that x += y can be read as x = x + y.

If you want to have a total score, you can:

# ^ Use the code above ^
total_value = sum(values) # It sum all the items of valuesprint(total_value)

# Or total new code.

total_score = 0
for tweet in tweets:
    for word in tweet:
        total_score += keywords.get(word, 0)
print(total_score)

Or if you want small codes:

total_value = sum([keywords.get(word,0) for tweet in tweets for word in tweet])

value = [sum([keywords.get(word, 0) for word in tweet]) for tweet in tweets]

Your choice.

Solution 2:

First we need to assign a variable for the value and set this to zero, then for each tweet and for every word inside this tweet we use the function dict.get() to get the corresponding value of word (if the word isn't in keywords it returns 0).

value = 0fortweetin tweets:
    forwordin tweet:
        value += keywords.get(word,0)

Post a Comment for "Matching Keywords In A Dictionary To A List In Python"