How to make tree of dictionaries?

Discussion in 'Python' started by Vlad Sirenko, Aug 12, 2003.

  1. Vlad Sirenko

    Vlad Sirenko Guest

    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
     
    Vlad Sirenko, Aug 12, 2003
    #1
    1. Advertising

  2. Vlad Sirenko

    Aahz Guest

    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
     
    Aahz, Aug 13, 2003
    #2
    1. Advertising

  3. "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
     
    Michael Peuser, Aug 13, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    2
    Views:
    1,648
    Roedy Green
    Aug 16, 2005
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,242
  3. Lad
    Replies:
    2
    Views:
    247
    Tim Chase
    Oct 19, 2006
  4. lysdexia
    Replies:
    6
    Views:
    561
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    516
    Brandon
    Aug 15, 2008
Loading...

Share This Page