Useful Notes and Links

Reynier Cruz-Torres, PhD

Python

Useful functions and commands:

sorted('weeav')

Output:

['a', 'e', 'e', 'v', 'w']

Collections

Python collections

import collections

Default dictionary

dict subclass that calls a factory function to supply missing values

ans = collections.defaultdict()

Counter

dict subclass for counting hashable objects

word = 'weerrtyuy'
count = collections.Counter(word)
Counter({'w': 1, 'e': 2, 'r': 2, 't': 1, 'y': 2, 'u': 1})

Here’s an implementation:

mydict = {}
for let in word:
    if let in mydict: mydict[let] += 1
    else: mydict[let] = 1
mydict