add indexes on the fly to lists

Diez B. Roggisch deetsNOSPAM at web.de
Mon Jan 24 16:33:27 EST 2005


The data structure you want to use is a dictionary - known as hashmap or
hash or map in other languages. Lets say you have a few objects of type
town that you want to access using their zip, then things could look like
this:

------------
class Town(object):
    def __init__(self, zip, cool_clubs):
        self.zip = zip
        self.colo_clubs = cool_clubs


towns = [Berlin(10439, ["Marietta", "ICON", "Bastart"]),
         Cologne(50000, ["Subway", "Stadtgarten", "Gebäude 9"])]

index = {}
for town in towns:
     index[town.zip] = town
----------------

Hope that makes it clear to you - look into the tutorial for a chapter about
python data structures.


-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list