Dictionary of Dicts question

George Sakkis george.sakkis at gmail.com
Fri Oct 17 17:57:43 CEST 2008

On Oct 16, 5:05 pm, John Townsend <jtown... at adobe.com> wrote:

> Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions.
> I have two text files. Each file contains data like this:
> Test file 1234 4567 8975
> I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text files are similar, but may not be exactly the same.
> My initial idea was to read the text files and turn each line into a dictionary entry. A dict for each text file. Then walk through the dicts and compare the numbers.
> If anyone has a better idea, I'd love to hear it.

If getting the diffs is the only thing you want to do with the data,
your idea is good enough. For more open-ended data manipulation you
should move to a database like in Dennis's reply but for a single
simple task a DB (even sqlite) is probably an overkill.

Here's a quick solution using the dicts approach:

import sys
from operator import sub

def read_data(path):
    data = {}
    for line in open(path):
        fields = line.split('\t')
        data[tuple(fields[:2])] = map(float,fields[2:])
    return data

d1 = read_data(sys.argv[1])
d2 = read_data(sys.argv[2])
for key in d1:
    if key in d2:
        diffs = map(sub, d1[key], d2[key])
        print key, diffs


More information about the Python-list mailing list