I have a table with headings A B C D. The values under D are indexed by those under A,B,C.
I also have a list of objects indexed by values contained in the A and B columns, i.e. (A,B).
For each object I want to write to a file all the entries in the table that have the same A,B index as my object.
This is what I'm doing:
prescriptions = {}
#Open ABCD table and create a dictionary mapping A,B,C to D
with open(table_file) as table:
reader = csv.reader(table, delimiter = '\t')
for row in reader:
code = (row[0], row[1], row[2])
prescriptions[code]=row[3]
for x in objects:
x_code = (x.A, x.B)
for p in prescriptions:
#check to see if A,B indices on x match those of the table entry
if p[0:2] == x_code:
row = prescriptions[p]
line = ",".join(p) +"," + row +"\n"
output.write(line)
This works. I get the exact output I want; however, when the table and the list get large, it takes an ungodly amount of time.
I'd love to modify my iterator (remove a p when I've found a match for it), but I know not to do that.
Is there anything I can do to speed this up?