Tellen gemaakt makkelijk

Datatypen in Python

Jason Myers

Instructor

Collections-module

  • Onderdeel van de standaardbibliotheek
  • Geavanceerde datacontainers
Datatypen in Python

Counter

  • Speciaal woordenboek om data te tellen, frequenties te meten
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
Datatypen in Python

Meest voorkomende met Counter

  • De methode .most_common() geeft waarden in aflopende volgorde
print(nyc_eatery_count_by_types.most_common(3))
[('Mobile Food Truck', 114), ('Food Cart', 74), ('Snack Bar', 24)]
Datatypen in Python

Laten we oefenen!

Datatypen in Python

Preparing Video For Download...