Dictionary & data
Garry Knight
garryknight at gmx.net
Tue Mar 16 17:26:32 EST 2004
In message <mqK5c.22047$8G2.9439 at fe3.columbus.rr.com>, kbass wrote:
> I have retrieved data from a database and I want to place this data into a
> dictionary or something like a HashMap (in Java). Can someone point me to
> an example?
Here's most of what you'll need to handle dictionaries:
d1 = {} # create empty dictionary
d2 = {"one": 1, "two": "two", 3: "three"} # create and populate dictionary
d3 = {1: 'spam', {'a': 'b'}} # nesting
d2["two"] = 42 # adding, changing
d3[1]['a'] = 'c' # changing value in nested dictionary
x = d2['two'] # fetch using index
y = d3[1]['a'] # fetch from nested dictionaries
cnt = len(d2) # get no. of key/value pairs
del d2[3] # remove key/value
for k in d2.keys(): # loop over keys
for v in d2.values(): # loop over values
for k,v in d2.items(): # loop over key/value pairs
d = d2.keys; d.sort() # get sorted list of keys
It's probably a good idea to work your way through the tutorial here:
http://www.python.org/doc/2.3.3/tut/tut.html
--
Garry Knight
garryknight at gmx.net ICQ 126351135
Linux registered user 182025
More information about the Python-list
mailing list