#!python import operator def read_text(fn): with open(fn, 'r') as f: lines = f.readlines() lookup = {} for line in lines: line = line.strip() total, fn = line.split(' ') lookup[fn] = int(total) return lookup new = read_text('new2.txt') old = read_text('new.txt') compare = {} for key in sorted(new.keys()): if key in old: diff = new[key] - old[key] compare[key] = diff sorted_compare = sorted(compare.items(), key=operator.itemgetter(1)) for key, diff in sorted_compare: print("{}\t{}".format(diff, key))