カウントを簡単に

Python のデータ型

Jason Myers

Instructor

collectionsモジュール

  • 標準ライブラリの一部
  • 高度なデータコンテナ
Python のデータ型

Counter

  • データのカウントや頻度測定に使用する特殊な辞書
from collections import Counter

nyc_eatery_count_by_types = Counter(nyc_eatery_types)
print(nyc_eatery_count_by_type)
Counter({'Mobile Food Truck': 114, 'Food Cart': 74, 'Snack Bar': 24,
'Specialty Cart': 18, 'Restaurant': 15, 'Fruit & Vegetable Cart': 4})
print(nyc_eatery_count_by_types['Restaurant'])
15
Python のデータ型

Counterで最頻値を検索

  • .most_common() メソッドはカウント値を降順で返す
print(nyc_eatery_count_by_types.most_common(3))
[('Mobile Food Truck', 114), ('Food Cart', 74), ('Snack Bar', 24)]
Python のデータ型

では、練習しましょう!

Python のデータ型

Preparing Video For Download...