Matching keywords in a dictionary to a list in Python

2024/11/23 8:35:11

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, 'excite': 10}

Following the dictionary are two tweets in a list in a list. For each tweet, we need to find which of the words from keywords are present in it.

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

The target is to find the sum of the keyword values found in each tweet line.

The code created needs to be a loop because there are more than 2 tweets. I do not understand how I should execute this process.

Appreciate your insight!

Answer

Try this:

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']]
total = 0for i in keywords:for j in tweets:if i in j:occourance = j.count(i)print('keyword=', i)total += keywords[i]*occourance
print('sum is: ', total)output:  keyword= bestkeyword= excitedsum is:  20
https://en.xdnf.cn/q/120650.html

Related Q&A

How to make case insensitive? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Webpage data extraction automation using python and selenium webdriver [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Asking the user if they want to play again [duplicate]

This question already has answers here:Ask the user if they want to repeat the same task again(2 answers)Closed 5 years ago.Basically its a guessing game and I have literally all the code except for th…

IF statement (checking for a string in a list) behave weirdly [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 10 years ago.This will probably be a dumb question, but why does this piece…

filteration in txt file in python

I have too many lines like this:>ENSG00000100206|ENST00000216024|DMC1|2371|38568257;38570043|38568289;38570286 CTCAGACGTCGGGCCGACGCAAGGCCACGCGCGCGAACACACAGGTGCGGCCCCGGGCCA CACGCACACCGTACAC >ENSG0…

Count the number of times elements in a numpy array consecutively satisfy a condition

I have a numpy array as follows:import numpy as np a = np.array([1, 4, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8])and a constant number b=6I am searching for a number c which is defined by t…

Finding the index of the first repeating item in an array in python

I was solving an arrays problem from GFG, where we need to find the index of the first repeating element. The question is as follows: Given an array arr[] of size n, find the first repeating element. T…

how to check str int list and tuple? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to take out numbers and add them together in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Populate mysql table with random data in python

How can create a mysql table in python and then populate it with random data.I want around 10000 rows and integer values will do work.