""" Histogram.py Author: David Kauchak Some functions for generating histogram-like data (text only) using dictionaries from any iterable data (e.g. lists or strings) """ def get_counts(data): """ Count how many times each item occurs in the sequence data""" counts = {} for d in data: if d in counts: counts[d] += 1 else: counts[d] = 1 return counts def print_counts(counts): """print out the counts from a dictionary, tab delimited""" for k in counts: print str(k) + "\t" + str(counts[k]) def print_counts_sorted(counts): """print out the counts from the data, sorted by the keys""" counts_keys = counts.keys() counts_keys.sort() for key in counts_keys: print str(key) + "\t" + str(counts[key])