Question1 During his presidential campaing in 2016, Donald Trump said 1 know wor
ID: 3730849 • Letter: Q
Question
Question1 During his presidential campaing in 2016, Donald Trump said 1 know words. I have the best words." Lets explore this idea in an objective, scientific manner. Write a function tweets_per_day(jsonurl) that, given the website address (URL) of a JSON file, counts the number of tweets that are sent on every day of the week. Return your result as a dictionary, mapping the day of the week to the total number of tweets sent on that day. For this question, you may want to open the file http://brain.mcmaster.ca/joey/trump_tweets.json and manually inspect the fields to help parse the data In J: import json import urllib.request url-"http://brain.mcmaster.ca/joey/trump_tweets.json response = urllib.request.urlopen(ur1) In I J: tweets_per_day('http://brain.mcmaster.ca/joey/trump_tweets.json) Friday: 33, Monday": 19, Saturday35, Sunday: 26, Thursday" 30, Tuesday: 28, Wednesday': 29 In [ ]: | # Hidden test for Q1a In [ ]: # Hidden test for Q1a In [ ]: # Hidden test for Q1aExplanation / Answer
url = 'http://brain.mcmaster.ca/joey/trump_tweets.json'
import urllib.request, json
with urllib.request.urlopen(url) as url_read:
data = json.loads(url_read.read().decode())
tweets_per_day ={}
for i in range (0,len(data)):
number.append(data[i]['day'])
import collections
tweets_per_day = dict(collections.Counter(number))
Output :
So The idea is simple - take the data out of the url, loop over it and see how many times each ['day'] key occurs. use collections.Counter to get the dictionary of numbers and use the dict keyword to create a dictionary
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.