473,385 Members | 1,942 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,385 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 2832
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.