| description | Dict in python! |
|---|
Helpful Libraries:
- defaultdict from collections
from collections import defaultdict
d = {}
print(d["car"]) # KEY ERROR!
dd = defaultdict(list)
print(dd["car"]) # []- Counter from collections
from collections import Counter
c = Counter(['eggs', 'ham', 'eggs', 'eggs', 'ham', 'bread'])
# c = { 'eggs': 3, 'ham': 2, 'bread': 1 }
c['bacon'] # 0 (count of a missing element is zero)
c.most_common(2) # [('eggs', 3), ('ham', 2)]
c.values()
c.keys()Use Counter and sort the answer keeping the most frequent in mind. Logic!