In [40]:
import requests
In [41]:
def my_mean(input_list):
list_sum = 0
list_count = 0
for el in input_list:
list_sum += el
list_count += 1
return list_sum / list_count
In [71]:
def my_median(input_list):
list_length = len(input_list)
if list_length%2 == 0:
return input_list[list_length/2]
else:
return input_list[round(list_length/2)]
In [72]:
api_key = "ffaf60d7d82258e112dd4fb2b5e4e2d6:3:72421680"
url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=gay+marriage&api-key=%s" % api_key
r = requests.get(url)
In [73]:
wc_list = []
for article in r.json()['response']['docs']:
wc_list.append(int(article['word_count'])) if article['word_count'] else []
In [80]:
wc_list = sorted(wc_list)
In [81]:
import numpy as np
In [82]:
my_mean(wc_list)
Out[82]:
In [83]:
np.mean(wc_list)
Out[83]:
In [84]:
my_median(wc_list)
Out[84]:
In [85]:
np.median(wc_list)
Out[85]: