473,378 Members | 1,699 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,378 software developers and data experts.

Newby Question: Python and A Nested Dictionary

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? The format that I am attempting is:

Select data ...
Get data ...
Place data into a nested dictionary
Rev[A] [b] = [500, 700, ...]
for value1 in a:
for value2 in Rev[A]
for value3 in Rev[A][b]
print something

kbass
Jul 18 '05 #1
2 2731
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

Jul 18 '05 #2

"Greg Ewing (using news.cis.dfn.de)" <wm*******@sneakemail.com> wrote in
message news:c2*************@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
Jul 18 '05 #3

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

Similar topics

2
by: John D. | last post by:
#This program gives: "SyntaxError: unqualified exec is not allowed # in function _ it contains a nested function with free variables" #I understand this is a scope problem(?) #I don't...
2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
9
by: jojoba | last post by:
hello! i am trying to come up with a simple way to access my values in my nested python dictionaries here is what i have so far, but i wanted to run it by the geniuses out there who might see...
4
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
0
by: gdrude | last post by:
I should start of by letting everyone know that I am not an experienced programmer. I have read a number of books on OOP languages and settled on Python to solve this particular problem. ...
0
by: Chris Rebert | last post by:
On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen@adobe.comwrote: Right, this clobbers the existing entry with this new blank one. This is evidenced by the fact that you're performing an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.