「Pythonエンジニア育成推進協会監修 Python実践レシピ - Mae向きなブログ」の続きです。
Chapter 6 テキストの処理
join
メソッドについて
words = '''Beautiful is better than ugly. Explicit is better than implicit.'''.split() print(words) print('-'.join(words[:5]))
Ruby
だと、words[...5].join('-')
と書いていたので、join
メソッドの使い方が新鮮に感じます。
- Unicode文字と文字の名前の変換
>>> import unicodedata >>> unicodedata.lookup('BEER MUG') '🍺' >>> unicodedata.name('🍺') 'BEER MUG'
Chapter 7 数値の処理
金額などの精度が求められる計算には、Decimal
を使うようにする。
>>> 100 * 1.10 110.00000000000001 >>> from decimal import Decimal >>> Decimal('100') * Decimal('1.10') Decimal('110.00')
Chapter 8 日付と時刻の処理
rrule
はカレンダーアプリケーションなどでよく使われる、繰り返しルールを指定するために使用する。
Chapter 9 データ型とアルゴリズム
デフォルト値を持った辞書 defaultdict
>>> from collections import defaultdict >>> dataset = [('九州', '福岡'), ('関東', '東京'), ('近畿', '大阪'), ('九州', '宮崎'), ('関東', '神奈川')] >>> d = defaultdict(list) >>> for region, prefecture in dataset: ... d[region].append(prefecture) ... >>> list(d.items()) [('九州', ['福岡', '宮崎']), ('関東', ['東京', '神奈川']), ('近畿', ['大阪'])]
Chapter 10 汎用OS・ランタイムサービス
CPU数を取得する。
>>> import os >>> os.cpu_count() 8