The ‘collections’ package in Python is a powerful tool to initiate text processing. It offers an alternative to Python’s general-purpose built-in containers — dict, list, set, and tuple. A traditional ...
Pythonに標準で付属するcollectionsモジュールには名前付きタプルをサポートするnamedtuple関数がある。これを使って名前付きタプルを使用する方法を紹介する。 名前付きタプルとは、その要素をインデックスではなく、名前で指定できるタプルのことだ。
collectionsモジュールのCounterクラスを使って、文字列やリストにどんな要素が何個含まれているかを簡単にカウントする方法を紹介する。 #item: foo, occurrence: 3 #item: baz, occurrence: 2 collections.Counterクラス Pythonに標準で付属するcollectionsモジュールにはCounterクラスが ...
Pythonの内部で高度に最適化されているから、データが何十万件あっても、自分でループを回すよりずっと速く正確に数えてくれるよ。 2. 人気ランキングも一瞬で作れる 集計したあと、「一番多いのはどれ? 」って調べたいときも、Counter はすごく優秀なんだ。
Pythonの辞書を扱う際、Keyが予め定まっていれば良いですが、動的にKeyを設定していきたいシーンがあると思います。そんな時、通常の辞書だとKeyErrorが起きてうまく値が入らないケースがありますが、defaultdictを使えば柔軟に対応が可能です。 まず通常の ...
Despite what many people think, Python's built-in list, dict, and deque are NOT thread-safe. They may be thread safe for some operations, but not all. This created a lot of confusion in the Python ...
Python's popularity stems from its simplicity, versatility, and the vast ecosystem of external libraries that extend its capabilities. These libraries allow developers to perform complex tasks without ...