Created
July 16, 2020 01:33
-
-
Save gamesbrainiac/6f4a7251e8d2dbd0ef6f460677490baa to your computer and use it in GitHub Desktop.
What does this package do? Episode 2: Enhanced Dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding=utf-8 | |
# What does this package do? Episode 2: collections.defaultdict | |
from collections import defaultdict | |
from pprint import pprint | |
events = [ | |
'mouse-click', | |
'mouse-hover', | |
'mouse-hover', | |
'button-hover', | |
'button-hover', | |
'button-click', | |
'mouse-move', | |
'leave-page', | |
'leave-page', | |
'leave-page', | |
'leave-page', | |
'enter-page', | |
'enter-page', | |
'enter-page', | |
'enter-page', | |
'mouse-click', | |
'mouse-click', | |
'mouse-click' | |
] | |
if __name__ == '__main__': | |
# We want to talk about frequency | |
event_frequency = defaultdict(int) | |
# We want to show how you can use a set | |
event_types = defaultdict(set) | |
# Counting frequency | |
for event in events: | |
event_frequency[event] += 1 | |
# Counting sub-types | |
for event in events: | |
e_type = event.split('-')[0] | |
event_types[e_type].add(event) | |
# Printing them out | |
pprint(event_frequency) | |
pprint(event_types) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment