[Tutor] mapping problem

Hugo González Monteverde hugonz-lists at h-lab.net
Wed Feb 1 21:45:12 CET 2006


What about choosing a native Python data structure for this?

A list of attributes comes to mind, as presence of one can be easily 
tested with

if x in attributes:
     do_something

Here's a test:

 >>> elements= {}
 >>> elements['boat'] = ['a', 'b', 'j', 'k']
 >>> 'a' in elements['boat']
True
 >>>

Then it is just a matter of getting the fiels into the structure, and 
then recursing through  the elements, like this.


elements = {}

for line in table_fileo:
     fields = line.split()

     #get rid of linefeed
     fields[-1] = fields[-1][:-1]

     elements[fields[-1]] = fields[:-1]

Then you get a dictionary with the last name in the table as key and a 
list of the other attributes as value.

Then, if you want to look for an attribute, just do:
for element in elements.items():
     if 'j' in element[1]:
         print element[0]

What do you think?

Hugo


More information about the Tutor mailing list