Re: Newby Question: Python and A Nested Dictionary

From: kbass (kbass_at_midsouth.rr.com)
Date: 03/12/04


Date: Fri, 12 Mar 2004 06:25:03 GMT


"Greg Ewing (using news.cis.dfn.de)" <wmwd2zz02@sneakemail.com> wrote in
message news:c2rc2l$1vtcg7$1@ID-169208.news.uni-berlin.de...
> kbass wrote:
> > I am new to Python and I am attempting to retrieve data from a database
and
> > I would like to place this data into a nested dictionary. After placing
the
> > data into a dictionary, I would like to loop through the data using for
> > loops. How would I do this?
>
> It's hard to say exactly how to go about building the dictionary
> without knowing more about the format of the data and how you
> want to structure it. Posting some sample data together with
> the dict you want to build from it would help.
>
> As for looping over the dictionaries, some general facts you
> might find useful are:
>
> for key in dictionary:
> ...
>
> will loop over all the keys in the dictionary.
>
> for value in dictionary.values():
> ...
>
> will loop over the values (but you won't know what
> their keys were).
>
> for key, value in dictionary.items():
> ...
>
> will loop over all the key/value pairs in the dictionary.
>
> --
> Greg Ewing, Computer Science Dept,
> University of Canterbury,
> Christchurch, New Zealand
> http://www.cosc.canterbury.ac.nz/~greg
>

An extra would be:

Data Source: Select region, source from accounts;

I want to be able to store the data from the select statement into something
like:
revenue [source][region = return data from the select statement

To loop over the data, I would like to perform something like:
    for src in revenue:
      for reg in revenue[source]:
        for value in revenue[source][region]
           print something

Hope this makes sense. Thanks!

kbass



Relevant Pages

  • Re: Writing huge Sets() to disk
    ... >> state and taking say 600MB, pushes it's internal dictionaries ... that this code takes a lot of extra memory. ... >> I believe it's the references problem, ... the swapspace reserved grows during that posted loop. ...
    (comp.lang.python)
  • Re: Does linq solve my problem
    ... (Yay for example driven testing! ... operators (in a short query like I proposed earlier) because all operators ... a loop and count on the fly, using a couple of dictionaries ... times however the requirement seems so unique, a loop would do. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Debugging whats wrong with a PS program using GhostScript
    ... The loop is now commented out because I through I'd go to ... arrays; but not dictionaries, for those use: ... reference loop then the === operator will print forever. ...
    (comp.lang.postscript)
  • Re: a question on python dict
    ... I made a mistake that I said the hash value should be recalculated ... each time the dict resize, what I wanted to say was that the position ... Creating the dictionary using a Python for loop: ... respectively so the situation does get progressively worse as dictionaries ...
    (comp.lang.python)
  • Re: Dictionary & data
    ... |> I have retrieved data from a database and I want to place this data into ... | Here's most of what you'll need to handle dictionaries: ...
    (comp.lang.python)