473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to make tree of dictionaries?

I need:
dict = {2002 : {'Jan': {1 : 'num1', 2: 'num2', 3 : 'num3'},
{'Feb': {1 : 'num4', 2: 'num5', 3 : 'num6'} } }
2003 : {'Jan': {1 : 'num7', 2: 'num8', 3 : 'num9'} } }

How to do it programmatically?
In Perl I would do something like:

while ($line = <>) {
if ($line =~ /^---\s+\w+\s+(\w+)\s+(\d*)\s+(\d+):(\d+):(\d+)\s+(\d+) \s+---$/) {
($month,$date,$hour,$minute,$sec,$year) = ($1,$2,$3,$4,$5,$6);
$statistics->{$year}->{$month}->{$date} += $sec;
}
}

But how to do it in Python without catching 'KeyError' exception or
iterating over some nested loops. How to do it elegantly?

--
WBR, VS

Jul 18 '05 #1
2 2210
In article <87************@big.terem>, Vlad Sirenko <zc****@bigmir.net> wrote:
I need:
dict = {2002 : {'Jan': {1 : 'num1', 2: 'num2', 3 : 'num3'},
{'Feb': {1 : 'num4', 2: 'num5', 3 : 'num6'} } }
2003 : {'Jan': {1 : 'num7', 2: 'num8', 3 : 'num9'} } }

How to do it programmatically?
In Perl I would do something like:

while ($line = <>) {
if ($line =~ /^---\s+\w+\s+(\w+)\s+(\d*)\s+(\d+):(\d+):(\d+)\s+(\d+) \s+---$/) {
($month,$date,$hour,$minute,$sec,$year) = ($1,$2,$3,$4,$5,$6);
$statistics->{$year}->{$month}->{$date} += $sec;
}
}

But how to do it in Python without catching 'KeyError' exception or
iterating over some nested loops. How to do it elegantly?


You don't need nested loops, but you do need multiple statements:

tmp_year = stats.setdefault(year, {})
tmp_month = tmp_year.setdefault(month, {})
tmp_month[date] = tmp_month.setdefault(date, 0) + sec
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #2

"Aahz" <aa**@pythoncraft.com> schrieb im Newsbeitrag
news:bh**********@panix1.panix.com...
In article <87************@big.terem>, Vlad Sirenko <zc****@bigmir.net> wrote:
I need:
dict = {2002 : {'Jan': {1 : 'num1', 2: 'num2', 3 : 'num3'},
{'Feb': {1 : 'num4', 2: 'num5', 3 : 'num6'} } }
2003 : {'Jan': {1 : 'num7', 2: 'num8', 3 : 'num9'} } }

How to do it programmatically?
In Perl I would do something like:

while ($line = <>) {
if ($line =~ /^---\s+\w+\s+(\w+)\s+(\d*)\s+(\d+):(\d+):(\d+)\s+(\d+) \s+---$/) { ($month,$date,$hour,$minute,$sec,$year) = ($1,$2,$3,$4,$5,$6);
$statistics->{$year}->{$month}->{$date} += $sec;
}
}

This is what I love Perl for!
BTW: Do you really mean += ?
This does nor match yourt example, neither seems to make much sense. Perhaps
a misconception?

But how to do it in Python without catching 'KeyError' exception or
iterating over some nested loops. How to do it elegantly?


You don't need nested loops, but you do need multiple statements:

tmp_year = stats.setdefault(year, {})
tmp_month = tmp_year.setdefault(month, {})
tmp_month[date] = tmp_month.setdefault(date, 0) + sec


*I* think this is less readable than the Python expression!
Note that *get* and *setdefault* are most powerful operations on mapping
types
once you get used to them, that is... ;)

Kindly
Michael P
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/
This is Python. We don't care much about theory, except where it intersects with useful practice. --Aahz

Jul 18 '05 #3

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

Similar topics

6
by: Kenneth McDonald | last post by:
I can't see anything about this in the notes on the upcoming 2.4 on python.org, but for some reason I thought I remembered seeing that a balanced tree sequence type would be included in Python in...
9
by: Kyle Root | last post by:
I'm working on a little practice program, but I've become stuck and am now utterly confused :? I've created a jumble of python modules, in each one is a tuple that goes something like, deps =...
1
by: Carl | last post by:
Dear friends, What are the best options for implementing fast and efficient trees in Python? The typical trees that I'm thinking of are those used by the finance industry to price different...
4
by: Rakesh | last post by:
Is there any structure/class with which I could represent Tree data structures in .NET ? Thanks Rakesh
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
0
by: webmaster | last post by:
TreeStages would like to announce the first evaluation release of XEntrant XML Editor 0.1, which is available for download from our website: http://www.treestages.com XEntrant is an XML Tree...
10
by: Sebastian Bassi | last post by:
I have a two column list like: 2,131 6,335 7,6 8,9 10,131 131,99 5,10
4
by: jm.suresh | last post by:
Hi, I have a tree data structure and I name each node with the following convention: a |---aa | |--- aaa | |--- aab | |---ab |
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.