Nested Dictionaries

def int_counts(ints):
    """Returns int-count dict as above."""
    counts = {}
    for num in ints:
        if not num in counts:
            counts[num] = 0
        counts[num] += 1
    return counts

def first_list(strs):
    """Return a firsts dict as above."""
    firsts = {}
    for s in strs:
        if len(s) >= 1:
            ch = s[0]
            if ch not in firsts:
                firsts[ch] = []
    return firsts

def suffix_list(strs):
    """Return a suffixes dict as above."""
    suffixes = {}
    for s in strs:
        if len(s) >= 2:
            # use suffix as key
            suffix = s[len(s)-2:]
            if suffix not in suffixes:
                suffixes[suffix] = []
    return suffixes

Word counts

def remove_vowels(s):
    out = ''
    for c in s:
        if c not in 'aieou':
            out += c
    return out

def remove_consonants(s):
    out = ''
    for c in s:
        if c in 'aieou':
            out += c
    return out

def count_lines(filename, keep_vowels):
    counts = {}
    with open(filename, 'r') as f:
        for word in f:
            word = word.strip()
            if keep_vowels:
                word = remove_vowels(word)
                word = remove_consonants(word)
            if word not in counts:
                counts[word] = 0
            counts[word] += 1
    for key in sorted(counts.keys()):
        print(key, '->', counts[key])

def main():
    args = sys.argv[1:]

    if len(args) == 2 and args[0] == '-vowels':
        count_lines(args[1], True)
        count_lines(args[0], False)

Big Tweet Data!

def add_tweet(user_tags, tweet):
    user = parse_user(tweet)
    if user == '':
        return user_tags

    # if user is not in there, put them in with empty counts
    if user not in user_tags:
        user_tags[user] = {}

    # counts is the nested tag -> count dict
    # go through all the tags and modify it
    counts = user_tags[user]
    parsed_tags = parse_tags(tweet)
    for tag in parsed_tags:
        if tag not in counts:
            counts[tag] = 0
        counts[tag] += 1

    return user_tags

def parse_tweets(filename):
    user_tags = {}
    # here we specify encoding 'utf-8' which is how this text file is encoded
    # python technically does this by default, but it's better to be explicit
    with open(filename, encoding='utf-8') as f:
        for line in f:
            add_tweet(user_tags, line)
    return user_tags

def user_total(user_tags, user):
    Optional. Given a user_tags dict and a user, figure out the total count
    of all their tags and return that number.
    If the user is not in the user_tags, return 0.
    if user not in user_tags:
        return 0
    counts = user_tags[user]
    total = 0
    for tag in counts.keys():
        total += counts[tag]
    return total



def flat_counts(user_tags):
    Given a user_tags dicts, sum up the tag counts across all users,
    return a "flat" counts dict with a key for each tag,
    and its value is the sum of that tag's count across users.
    counts = {}
    for user in user_tags.keys():
        tags = user_tags[user]
        for tag in tags:
            if tag not in counts:
                counts[tag] = 0
            counts[tag] += tags[tag]
    return counts