473,287 Members | 1,418 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,287 software developers and data experts.

Re: multi dimensional dictionary

Alok Kumar wrote:
Dear All,

I am using dictionary for filling my xpath parsed data.

I wanted to use in the following manner.

mydict[index] ["key1"] ["key2"] #Can someone help me with right
declaration.

So that I can fill my XML xpath parsed data

mydict[0] ["person"] ["setTime"] = "12:09:30"
mydict[0] ["person"] ["clrTime"] = "22:09:30"

That kind of thing can be done, but there's a better way perhaps:

Create keys for a single level dictionary which are tuples composed of
the sequence of keys you would have used in your multidimensional
dictionary.

mydict[(0,"person","setTime")] = "12:09:30"
mydict[(0,"person","clrTime")] = "22:09:30"

Would that work for you?

Gary Herron

>
Can someone help me with right declaration usages. Your help will be
highly appreciated.

Regards
Alok
------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
3 2830
Gary Herron <gh*****@islandtraining.comwrites:
mydict[(0,"person","setTime")] = "12:09:30"
mydict[(0,"person","clrTime")] = "22:09:30"
Note that this is more succinctly written as:

mydict[0, "person", "setTime"] = "12:09:30"

with the added advantage that it looks like a multi-dimensional array.
:-)

The only problem with this approach is that when you want to iterate
over, say, mydict[0], or mydict[0]["person"], it's not possible
without traversing the entire dict.
Jun 27 '08 #2
Gary Herron wrote:
Alok Kumar wrote:
>Dear All,

I am using dictionary for filling my xpath parsed data.

I wanted to use in the following manner.

mydict[index] ["key1"] ["key2"] #Can someone help me with right
declaration.

So that I can fill my XML xpath parsed data

mydict[0] ["person"] ["setTime"] = "12:09:30"
mydict[0] ["person"] ["clrTime"] = "22:09:30"
[I didn't see the original post]
>>from collections import defaultdict
def make_inner():
.... return defaultdict(lambda: defaultdict(make_inner))
....
>>mydict = make_inner()
mydict[0]["person"]["setTime"] = "12:09:30"
mydict[0]["person"]["shoes"]["color"] = "bright yellow"
mydict
defaultdict(<function <lambdaat 0x2b7afd0025f0>, {0: defaultdict(<function
make_inner at 0x2b7afd002578>, {'person': defaultdict(<function <lambdaat
0x2b7afd002668>, {'setTime': '12:09:30', 'shoes': defaultdict(<function
make_inner at 0x2b7afd002578>, {'color': 'bright yellow'})})})})

If that looks too messy, try a subclass:
>>class Dict(defaultdict):
.... def __init__(self):
.... defaultdict.__init__(self, Dict)
.... def __repr__(self):
.... return dict.__repr__(self)
....
>>mydict = Dict()
mydict[0]["person"]["setTime"] = "12:09:30"
mydict
{0: {'person': {'setTime': '12:09:30'}}}
>>mydict[0]["person"]["shoes"]["color"] = "bright yellow"
mydict
{0: {'person': {'setTime': '12:09:30', 'shoes': {'color': 'bright
yellow'}}}}

Peter
Jun 27 '08 #3
On May 28, 3:11*am, Peter Otten <__pete...@web.dewrote:
Gary Herron wrote:
Alok Kumar wrote:
Dear All,
I am using dictionary for filling my xpath parsed data.
I wanted to use in the following manner.
mydict[index] ["key1"] ["key2"] * *#Can someone help me with right
declaration.
So that I can fill my XML xpath parsed data
mydict[0] ["person"] ["setTime"] = "12:09:30"
mydict[0] ["person"] ["clrTime"] = "22:09:30"

[I didn't see the original post]
>from collections import defaultdict
def make_inner():

... * * return defaultdict(lambda: defaultdict(make_inner))
...>>mydict = make_inner()
>mydict[0]["person"]["setTime"] = "12:09:30"
mydict[0]["person"]["shoes"]["color"] = "bright yellow"
mydict
<snip>

When this has come up in previous threads, I think this was the best
solution that was proposed:
from collections import defaultdict

class recursivedefaultdict(defaultdict):
def __init__(self):
self.default_factory = type(self)
Here is this recursivedefaultdict in action:

data = [
('A','B','Z',1),
('A','C','Y',2),
('A','C','X',3),
('B','A','W',4),
('B','B','V',5),
('B','B','U',6),
('B','D','T',7),
]

table = recursivedefaultdict()

for k1,k2,k3,v in data:
table[k1][k2][k3] = v

for kk in sorted(table.keys()):
print "-",kk
for jj in sorted(table[kk].keys()):
print " -",jj
for ii in sorted(table[kk][jj].keys()):
print " -",ii,table[kk][jj][ii]

Prints:

- A
- B
- Z 1
- C
- X 3
- Y 2
- B
- A
- W 4
- B
- U 6
- V 5
- D
- T 7

-- Paul

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...

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.