How can I loop through a list of strings and extract out three different variables? The question I am trying to solve was part of a course but no solution was given.
Question: How to loop through a set of tweets and take out three different variables: "happy_tweets", "sad_tweets", and "neutral_tweets"
I am having a lot of trouble figuring out the best way to loop through the tweets and extract out each of these types of tweets.
tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML #zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python. #zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
happy_tweets = 0
sad_tweets = 0
neutral_tweets = 0
for tweet in tweets:
if happy_words in tweets:
happy_tweets += 1
print(happy_tweets)