[Python] Dictionary (hash)

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

[Python] Dictionary (hash)
Python dictionary
[Python] dictionary
Python dictionary
[Python] Memo dictionary
Python basics: dictionary
[Python] Each hash
Hash in Perl is a dictionary in Python
Python Basic Course (7 Dictionary)
Python3 List / dictionary memo
Python Dictionary Beginner's Guide
Python list, for statement, dictionary
Create a dictionary in Python
Python
Python> dictionary / collections> defaultdict () / Counter ()
Avoid KeyError in python dictionary
Python> dictionary> get ()> optional value
Summary of Hash (Dictionary) operation support for Ruby and Python
Expansion by argument of python dictionary
Replace dictionary value with Python> update ()
Python for super beginners Python # dictionary type 1 for super beginners
Hash method (open address method) in Python
Basic grammar of Python3 system (dictionary)
Python for super beginners Python # dictionary type 2 for super beginners
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python> dictionary> values ()> Get All Values by Using values ()
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
Convert Python> two value sequence to dictionary
python function ①
Python basics
Slowly hash passwords using bcrypt in Python
ufo-> python (3)
Python comprehension
install python
Python Singleton
python memo
Python Jinja2
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
Dictionary type 2
[Python] Variables
Python functions
Python sys.intern ()