lundi 18 mai 2015

Elegant way to test list of if statements in Python

I have some spreadsheets represented as a list of lists in python, and I'm generating output from those.

However, I end up with some really ugly code when I have to omit sections of the sheet, such as: if not "string" in currentline[index] and not "string2" in currentline[index] and not... and so on.

Is it possible to represent all the conditions as a list of tuples, say omit = [(0, "foo"), (5,"bar)] and then have one if statement that checks of both statements are false?

If I have these two lists:

list = [["bar","baz","foo","bar"],["foo","bar","baz","foo","bar"]]
omit = [(0,"foo"),(4,"bar")]

and I only want the first one to print, I need an if statement to test every condition inside omit somehow, something like:

for idx, condition in enumerate(omit):
    a, b = omit[idx]
    if list[a] != omit[b] for all pairs of a and b in omit:
        print list

Aucun commentaire:

Enregistrer un commentaire