Since it was a big deal to learn Python, I investigated how to write it like Python. Python-ness agrees with Python's efficient code.
Referenced slides https://speakerdeck.com/pyconslides/transforming-code-into-beautiful-idiomatic-python-by-raymond-hettinger-1 http://kesin.hatenablog.com/entry/2013/05/12/004541
This is just like Python
, and how to use a dictionary is a very important and basic technique.
There seems to be a good way to take care of a dictionary.
You can loop by key, either by specifying a dictionary or by using the keys
method of the dictionary object.
d = {'kaijo': 'red', 'shinmei': 'blue', 'ooiwa': 'yellow', 'pegie': 'pink', 'asuka': 'green'}
for k in d:
print(k)
for k in d.keys():
print(k)
As a method of looping the key and the value, it is possible to loop by the upper key and get the value from the key. Python dictionaries have a ʻitems` method that eliminates the need to retrieve values from the dictionary using keys. In addition, the variable of the value can be freely decided, which makes the code easy to read.
d = {'kaijo': 'red', 'shinmei': 'blue', 'ooiwa': 'yellow', 'pegie': 'pink', 'asuka': 'green'}
for k in d:
print(k, '->', d[k])
for k, v in d.items():
print(k, '->', v)
If you already have a list of keys and a list of values, you can combine them to create a dictionary.
You can do it without using the for
statement.
from itertools import izip
keys = ['kaijo', 'shinmei', 'ooiwa', 'pegie', 'asuka']
values = ['red', 'blue', 'yellow', 'pink', 'green']
d = dict(izip(keys, values))
If you want to make a dictionary with index-like serial numbers, you can create it by using ʻenumerate`.
values = ['red', 'blue', 'yellow', 'pink', 'green']
d = dict(enumerate(values))
When processing how many times the same word appears in an English sentence, it is easy to imagine the following processing.
colors = ['red', 'green', 'red', 'blue', 'green', 'red']
d = {}
for color in colors:
if not color in d:
d[color] = 0
d[color] += 1
The get
method of the dictionary returns how many keys you have specified, but if there are no keys, it returns the value given as an argument.
You can use it to write as follows.
colors = ['red', 'green', 'red', 'blue', 'green', 'red']
d = {}
for color in colors:
d[color] = d.get(color, 0) + 1
The defaultdict
of the collections
module has the ability to add if there is no key.
You can also use it to write as follows.
It is easier to read than using get
.
from collections import defaultdict
colors = ['red', 'green', 'red', 'blue', 'green', 'red']
d = defaultdict(int)
for color in colors:
d[color] += 1
For example, if you have a list of words and want to group them by the number of characters in the word, there is a simple code to set a default value (in this case, a list that stores names) when there is no key.
You can use the setdefault
method to store the default value.
names = ['raymond', 'rachel', 'matthew', 'ronger', 'betty', 'melissa', 'judith', 'charlie']
d = {}
for name in names:
key = len(name)
if not key in d:
d[key] = []
d[key].append(name)
d2 = {}
for name in names:
key = len(name)
d2.setdefault(key, []).append(name)
This can be written as follows using the previous defaultdict
.
The ʻif` statement is gone and it's elegant.
from collections import defaultdict
names = ['raymond', 'rachel', 'matthew', 'ronger', 'betty', 'melissa', 'judith', 'charlie']
d = defaultdict(list)
for name in names:
key = len(name)
d[key].append(name)
Recommended Posts