Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to make tree of dictionaries?

Reply
Thread Tools

How to make tree of dictionaries?

 
 
Vlad Sirenko
Guest
Posts: n/a
 
      08-12-2003
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

 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      08-13-2003
In article <>, Vlad Sirenko <> 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 () <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
 
Reply With Quote
 
 
 
 
Michael Peuser
Guest
Posts: n/a
 
      08-13-2003

"Aahz" <> schrieb im Newsbeitrag
news:bhc9bh$lp9$...
> In article <>, Vlad Sirenko <>

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 () <*>

http://www.pythoncraft.com/
>
> This is Python. We don't care much about theory, except where it

intersects
> with useful practice. --Aahz



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
B+ Tree versus Ternary Search Tree Ramkumar Menon Java 2 08-16-2005 08:13 PM
B+ Tree versus Ternary Search Tree Ramkumar Menon Java 0 08-16-2005 09:01 AM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM
Spanning Tree And Per Vlan Spanning Tree Amy L. Cisco 0 07-24-2003 10:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57