import csv


def main(file_in, file_out):
    """
    Read in lines, flatten list of lines to list of words,
    sort and tally words, write out tallies.
    """

    with open(file_in, 'r') as f_in:
        word_lists = [line.split() for line in f_in]

    # Flatten the list
    # http://stackoverflow.com/questions/952914/\
    # making-a-flat-list-out-of-list-of-lists-in-python
    words = [word for word_list in word_lists for word in word_list]
    counts = map(words.count, words)
    tallies = sorted(set(zip(words, counts)))

    with open(file_out, 'w') as f_out:
        writer = csv.writer(f_out, quoting=csv.QUOTE_NONNUMERIC)
        for (word, count) in tallies:
            writer.writerow([word, count])

    return None