Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# Sample Twitter feed tweets = [ Happy #IlliniFriday!, It is a pretty campus, isn't it , #illini?, Diving into the last weekend of

# Sample Twitter feed
tweets =[
"Happy #IlliniFriday!",
"It is a pretty campus, isn't it, #illini?",
"Diving into the last weekend of winter break like... #ILLINI #JoinTheFight",
"Are you wearing your Orange and Blue today, #Illini Nation?"
]
# Dictionary to store hashtag counts
hashtag_counts ={}
# Iterate through each tweet
for tweet in tweets:
# Split the tweet into words
words = tweet.split()
# Iterate through the words to find hashtags
for word in words:
# Check if the word starts with a '#'
if word.startswith('#'):
# Remove special characters and convert to lowercase
hashtag = word.lower()
# Update the count in the dictionary
hashtag_counts[hashtag]= hashtag_counts.get(hashtag,0)+1
# Sort the dictionary by count (descending) and then by hashtag (ascending)
sorted_hashtag_counts = sorted(hashtag_counts.items(), key=lambda x: (-x[1], x[0]))
# Print the sorted hashtag counts
print(sorted_hashtag_counts)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions