Zählen leicht gemacht

Datentypen in Python

Jason Myers

Instructor

Modul Collections

  • Teil der Standardbibliothek
  • Erweiterte Datencontainer
Datentypen in Python

Counter

  • Spezielles Dictionary zum Zählen und für Häufigkeiten
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
Datentypen in Python

Counter: Häufigstes finden

  • Mit .most_common() erhältst du Werte absteigend sortiert
print(nyc_eatery_count_by_types.most_common(3))
[('Mobile Food Truck', 114), ('Food Cart', 74), ('Snack Bar', 24)]
Datentypen in Python

Lass uns üben!

Datentypen in Python

Preparing Video For Download...