In [1]:
import requests # a better package than urllib2
In [2]:
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 [81]:
def my_median(input_list):
input_list.sort()
list_length = len(input_list)
if list_length % 2 == 1:
index = int((list_length - 1) / 2)
return input_list[index]
else:
index1 = int(list_length/2)
index2 = int(index1 - 1)
return (input_list[index1] + input_list[index2]) / 2
In [4]:
api_key = "ffaf60d7d82258e112dd4fb2b5e4e2d6:3:72421680"
In [6]:
url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=gay+marriage&api-key=%s" % api_key
In [7]:
r = requests.get(url)
In [22]:
# I'm not counting the things without word counts, because I don't think they should count as articles
wc_list = []
for article in r.json()['response']['docs']:
if article['word_count']:
wc_list.append(int(article['word_count']))
In [82]:
my_mean(wc_list)
Out[82]:
In [83]:
import numpy as np
In [84]:
np.mean(wc_list)
Out[84]:
In [85]:
my_median(wc_list)
Out[85]:
In [86]:
np.median(wc_list)
Out[86]: