<[email protected]> wrote
I am trying to output a list of addresses that do not match a list of State abbreviations. What I have so far is:

def main():
    infile = open("list.txt", "r")
    for line in infile:
        state = line[146:148]
omit_states = ['KS', 'KY', 'MA', 'ND', 'NE', 'NJ', 'PR', 'RI', 'SD', 'VI', 'VT', 'WI']
        for n in omit_states:
            if state != n:
                print line

This prints it for every entry in omit_states. You probably want to add a break command after the print


But I would do this with a list comprehension or generator expression (depending on your Python version):


lines = [line for line in infile if line[146:148] not in omit_states]
print '\n'.join(lines)

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to