473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

RE: Dictionary of Dicts question

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
4 1539
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
14
by: Antoon Pardon | last post by:
I'm writing a Tree class, which should behave a lot like a dictionary. In order to test this, I took the unittest from the source distribution for dictionaries and used it to test against my Tree...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
5
by: Jon Smirl | last post by:
Is there some way to tell a dictionary object that I am going to load 1M objects into it and have it pre-allocate enought slots to hold all of the entries? Thus avoiding many thousand memory...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
5
by: Greg Corradini | last post by:
Hello All, I'm attempting to create multiple dictionaries at once, each with unique variable names. The number of dictionaries i need to create depends on the length of a list, which was returned...
0
by: Chris Rebert | last post by:
On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen@adobe.comwrote: Right, this clobbers the existing entry with this new blank one. This is evidenced by the fact that you're performing an...
14
by: Prateek | last post by:
I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.