Question: # 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!