Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write Python code to count the frequency of hashtags in a twitter feed. Your code assumes a twitter feed variable tweets exists, which is a

image text in transcribedimage text in transcribed

Write Python code to count the frequency of hashtags in a twitter feed. Your code assumes a twitter feed variable tweets exists, which is a list of strings containing tweets. Each element of this list is a single tweet, stored as a string. For example, tweets may look like: 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?") Your code should produce a sorted list of tuples stored in hashtag_counts, where each tuple looks like (hashtag, count), hashtag is a string and count is an integer. The list should be sorted by count in descending order, and if there are hashtags with identical counts, these should be sorted alphabetically, in ascending order, by hashtag. From the above example, our unsorted hashtag_counts might look like: [('#illini', 2), ('#jointhefight', 1), ('#illinifriday!', 1), ('#illini?', 1)] The hashtag_counts sorted by the above specifications will look like: [('#illini', 2), ('#illini?', 1), ('#illinifriday!', 1), ('#jointhefight', 1)] You may use str.split() to split each tweet into a list of words. A hashtag is any word that starts with a hash mark (#). (That means that the hash mark # should be included in the hashtag value above.) The setup code gives the following variable: Name tweets Type list Description a list of strings containing tweets Your code snippet should define the following variable: Name Type Description hashtag_counts list list of tuples (hashtag,count) where hashtag is a string and count is an integer

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