472,354 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Re: Dictionary of Dicts question

On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jt******@adobe.comwrote:
I'm working with a Dictionary of Dicts. Something like this:

myDict = {
'TestName': {
'FileName':{

'ct_init':1234,

'psl_init':5678,

'total_test_time':7890,

'psl_shutdown':8765,

'ct_shutdown':9021,

'total_time':3421,
},
}
}

Accessing values is pretty straightforward (nice change from my Perl days).
For example:

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

in Python interpreter yields

9021

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': {}, }

I get

{'TestName': {'NewFileName': {}}}
Right, this clobbers the existing entry with this new blank one. This
is evidenced by the fact that you're performing an _assignment_ on a
dictionary key rather than calling a _mutator_ method on a dictionary
value. A dictionary has only one value for a given key (but
importantly, that value can be a list).
>
So, how do I add a new entry without replacing the old entry?
Switch to a Dict of Lists of Dicts and append to the appropriate list
when adding the new entry, or preferably, start using objects instead
of ad-hoc nested dictionaries.

Regards,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks

John Townsend (5-7204),
AGM-FL and PSL QE Lead

--
http://mail.python.org/mailman/listinfo/python-list

Oct 16 '08 #1
0 911

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...
4
by: John Townsend | last post by:
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...
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.