mardi 28 avril 2015

Split list item and remove split items if both are not contained in another list in Python

I have a file, called "nwk", with two values in each row. I would like to use .split to separate the values of each row, and then compare both of those values to another list of integers, called "vals". If both of the values in a row in nwk are not contained in vals, I would like to remove that row from nwk. Here is what I have so far:

for line in nwk:
    a = [ (int(n) for n in line.split()) ]
    if a[0] not in vals:
        nwk.remove(line)
    else:
        if a[1] not in vals:
            nwk.remove(line)
        else:
            continue

However, when I print nwk, the code has merely removed 1/2 of my original lines in nwk, which I know to be incorrect. How may I fix this? Thank you in advance for your help.

Aucun commentaire:

Enregistrer un commentaire