vendredi 27 septembre 2019

How do I set up an if statement to use an array of conditionals as input in python

First I check a given condition, as per a normal IF statement. If it is True, I need to scan all files for a single keyword. If it is not True, I need to scan all files for a set of keywords, which will have been supplied.

For my simple working example, rather than scanning file names in a directory, I simply search a string for the keywords.

test1 = "NotKeyWord"
password = "password"
password1 = "password1"
password2 = "password2"
password3 = "password3"

if test1.lower() == "keyword":
    condition = password
else:
    condition = [password1, password2, password3]

f = "password1_password2_password3_jibberish_E=mc2"
if condition in f:
    print("Problem solved")

If the keyword = "keyword" i.e., is singular, then this code works. Fine. However, I would like to make it so that if it is the other case, which requires knowing several keywords to grab the right file, I don't need to resort to explicitly writing out all the words.

For my purposes I could write

if password1 in f and password2 in f and password3 in f:
    print("problem solved")

But I am after a pythonic method, that will hopefully be generic enough to be able to handle an array of keywords that is of unknown length, and thus can't be hard coded.

Aucun commentaire:

Enregistrer un commentaire