In Python, a hash (more precisely, a hash table) is called a dictionary.
Counter and defaultdict, such as when using the dictionary function to count the number of occurrences of each KW. It is good to use a subclass of the dictionary (/ //docs.python.jp/2/library/collections.html#collections.defaultdict). For example, if Counter and defaultdict are not used, redundant description is required as shown below.
sample1.py
# -*- coding: UTF-8 -*-
#Dictionary initialization
count_by_kw = {}
#KeyError
count_by_kw['apple'] += 1
#It's verbose, but you need to write:
if count_by_kw.get('apple'):
count_by_kw['apple'] += 1
else:
count_by_kw['apple'] = 1
If you use Counter, you can simply describe this as follows. You can also use the most_common method to retrieve keys in descending order of value.
sample2.py
# -*- coding: UTF-8 -*-
from collections import Counter
#Dictionary initialization
count_by_kw = Counter()
#Can be a simple description
count_by_kw['apple'] += 1
count_by_kw['apple'] += 1
count_by_kw['apple'] += 1
count_by_kw['peach'] += 1
count_by_kw['orange'] += 1
count_by_kw['orange'] += 1
#Value in descending order (apple-> orange ->Take out the key to peach)
for kw, count in count_by_kw.most_common():
print(kw, count)
Furthermore, by combining Counter and defaultdict, it is possible to create a dictionary of dictionaries. Of course, you can also use the most_common method.
sample3.py
# -*- coding: UTF-8 -*-
from collections import Counter
from collections import defaultdict
#Dictionary initialization
count_by_kw = defaultdict(Counter)
#Dictionary dictionary
count_by_kw['category1']['apple'] += 1
count_by_kw['category1']['apple'] += 1
count_by_kw['category1']['orange'] += 1
#Extract key and value from the outer dictionary, and then extract key and value from the inner dictionary
for kw1, counter in count_by_kw.items():
for kw2, count in counter.most_common():
print(kw1, kw2, count)
Recommended Posts