輕鬆做計數

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...