处理usa.gov数据
导入数据
import json
path = 'usagov_bitly_data2012-03-16-1331923249.txt'
records = [json.loads(line) for line in open(path)]
对时区进行计数
因为不是所有记录都有时区字段,所以必须加入if判断,否则报错。
# time_zones = [rec['tz'] for rec in records]
time_zones = [rec['tz'] for rec in records if 'tz' in rec]
- 计数函数
# 方法1
def get_counts(sequence):
counts = {
}
for x in sequence:
if x in counts:
counts[x] += 1
else:
counts[x] = 1
return counts
# 得到前10位的时区及其计数值
def top_counts(count_dict, n=10):
value_key_pairs = [(count, tz) for tz, count in count_dict.items()]
value_key_pairs.sort()
return value_key_pairs[-n:]
counts = get_counts(time_zones)
print (counts['America/New_York'])
print(len(time_zones))
# 方法2
from collections import defaultdict
def get_counts2(sequence):
counts = defaultdict(int) # values will initialize to 0
for x in sequence:
counts[x] += 1
return counts
# 方法3
from collections import Counter
co