自定义函数计算每个类型出现的次数
def get_counts(sequence):
counts = {}
for x in sequence:
if x in counts:
counts[x] += 1
else:
counts[x] = 1
return counts
from collections import defaultdict
def get_counts2(sequence):
counts = defaultdict(int)
for x in sequence:
counts[x] += 1
return counts
tz_counts = frame['tz'].value_counts()
tz_counts[:10]
从效率上来说,我更推荐方案二。
似乎感觉到这是某个案例的拓展。对了,是判断一个元素是否在一个列表中。引入字典模式,就可用在计算频次上。
def check(x,L):
if x in L:
return True
else:
L.append(x)
def top_counts(count_dict, n):
value_key_pairs = [(count, tz) for tz, count in count_dict.items()]
value_key_pairs.sort()
return value_key_pairs[-n:]
from collections import Counter
counts = Counter(time_zones)
counts.most_common(n)
未完待续