By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,317 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,317 IT Pros & Developers. It's quick & easy.

RE: Dictionary of Dicts question

P: n/a
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.

-----Original Message-----
From: Joe Strout [mailto:jo*@strout.net]
Sent: Thursday, October 16, 2008 1:49 PM
To: John Townsend
Subject: Re: Dictionary of Dicts question

On Oct 16, 2008, at 1:19 PM, John Townsend wrote:
Accessing values is pretty straightforward (nice change from my Perl
days). For example:

myDict['TestName']['FileName']['ct_shutdown']

in Python interpreter yields

9021
FWIW, I'd recommend you scrap this dict-of-dicts structure and instead
define a class (or three). You'll know you have it right when instead
of the above, you're typing something like:

myDict['TestName'].FileName.ct_shutdown

(The division might be a little different -- it's not entirely clear
to me what in your sample code is just place-holder names, and what is
meant literally.)
However, when I try to add, let's say, a new FileName entry, I end
up replacing the previous FileName entry.

In Python interpreter, I try:

myDict['TestName'] = {'NewFileName': {}, }
Well, yes, this code says "stuff the new dictionary {'NewFileName':{}}
into myDict under 'TestName', replacing whatever was under 'TestName'
before."

If that's not what you want, then don't do that -- assign to
myDict['TestName2'] or some such.
So, how do I add a new entry without replacing the old entry?
You don't; that's the whole point of dictionaries. A dictionary maps
keys to values. You can't have two values for the same key. You
could have a key map to a list, but I suspect we're straying a bit far
now from whatever it is you're trying to accomplish.

If you can explain what it is you're actually trying to accomplish
(without reference to implementation details like dictionaries and
entries), maybe we can suggest a suitable approach.

Best,
- Joe

Oct 16 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Here are some sample lines.

Text file 1 contains:

DescribeImage AllAdjustments.psd 0.668000012636 0.046 0.426 0.06475 0.06475 0.005875
DescribeImage All_Options_Multi.psd 0.552750021219 0.046 0.355875 0.01525 0.017125 0.0
DescribeImage All_Options_Quad.psd 0.57025000453 0.046 0.314875 0.058375 0.058375 0.007875
DescribeImage Apple_RGB.psd 0.538999974728 0.046 0.315 0.04675 0.04875 0.0

Text file 2 contains:
DescribeImage AllAdjustments.psd 0.7889 0.056 0.786 0.0665 0.06476 0.999
DescribeImage All_Options_Multi.psd 0.5527500421419 0.43154312 0.4443 0.43124 0.017125 0.0
DescribeImage All_Options_Quad.psd 0.5702503200453 0.046 0.34 0.058375 0.4342 0.43214

Lines are tab delimited. Note, in this example text file 2 contains three lines, while text file 1 contains four lines.

Where there are matching lines in each text file (e.g. "DescribeImage AllAdjustments.psd" exists in both files), I want to compare each of the numbers from that line to the numbers in the corresponding line.

If there is not corresponding line (like " DescribeImage Apple_RGB.psd"), skip the comparison test.

I hope this helps describe my problem.

-----Original Message-----
From: py************************************@python.org [mailto:py************************************@pyth on.org] On Behalf Of Dennis Lee Bieber
Sent: Thursday, October 16, 2008 2:44 PM
To: py*********@python.org
Subject: Re: Dictionary of Dicts question

On Thu, 16 Oct 2008 14:05:16 -0700, John Townsend <jt******@adobe.com>
declaimed the following in comp.lang.python:
>
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.
This would be easier if you gave a few lines sample from each file
and the expected output of your processing of those lines.
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.
Use a relational database and "GROUP BY" selections?
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
--
http://mail.python.org/mailman/listinfo/python-list
Oct 16 '08 #2

P: n/a
On Oct 16, 11:03*pm, John Townsend <jtown...@adobe.comwrote:
Here are some sample lines.

Text file 1 contains:

DescribeImage * AllAdjustments.psd * * *0.668000012636 *0.046 * 0.426 * 0.06475 0.06475 0.005875
DescribeImage * All_Options_Multi.psd * 0.552750021219 *0.046 * 0..355875 * * * *0.01525 0.017125 * * * *0.0
DescribeImage * All_Options_Quad.psd * *0.57025000453 * 0.046 *0.314875 * * * *0.058375 * * * *0.058375 * * * *0.007875
DescribeImage * Apple_RGB.psd * 0.538999974728 *0.046 * 0.315 *0.04675 0.04875 0.0

Text file 2 contains:
DescribeImage * AllAdjustments.psd * * *0.7889 *0.056 * 0.786* 0.0665 *0.06476 0.999
DescribeImage * All_Options_Multi.psd * 0.5527500421419 0.43154312 * * *0.4443 *0.43124 0.017125 * * * *0.0
DescribeImage * All_Options_Quad.psd * *0.5702503200453 0.046 * 0..34 * *0.058375 * * * *0.4342 *0.43214

Lines are tab delimited. Note, in this example text file 2 contains threelines, while text file 1 contains four lines.

Where there are matching lines in each text file (e.g. "DescribeImage *AllAdjustments.psd" exists in both files), I want to compare each of the numbers from that line to the numbers in the corresponding line.

If there is not corresponding line (like " DescribeImage * * * *Apple_RGB.psd"), skip the comparison test.

I hope this helps describe my problem.
If the first 2 fields are unique when combined, then you could use
that as the key:

data = {}
for line in open(path):
fields = line.split("\t")
data[tuple(fields[ : 2])] = fields[2 : ]
Oct 17 '08 #3

P: n/a
MRAB:
for line in open(path):
fields = line.split("\t")
data[tuple(fields[ : 2])] = fields[2 : ]
Keeping the key as a string may have some memory/performance
advantages (not tested):

for line in open(path):
fields = line.split("\t")
data[fields[0] + fields[1]] = map(float, islice(fields, 2, None))

Or probably faster (not tested):

for line in open(path):
parts = s.rsplit("\t", 6)
data[parts[0]] = map(float, islice(parts, 1, None))

Or (not tested):

for line in open(path):
parts = s.rsplit("\t", 6)
data[parts[0]] = [float(parts[i]) for i in xrange(1, 7)]

Having a built-in xsplit/xsplitr method here probably helps
significantly.
If the FP numbers are really precise then you can compare them as
strings too, but that's quite unsafe.

Bye,
bearophile
Oct 17 '08 #4

P: n/a
On Oct 16, 5:05*pm, John Townsend <jtown...@adobe.comwrote:
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

HTH,
George
Oct 17 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.