Falseif any one item in the list matched that check. Oh, and if the list was empty, that had to be addressed appropriately as well. And I wanted to do it in the smallest way possible.
Originally I'd hacked together a short four or five line function that did the job. However, after coming back to the code for some other updates, I noticed that said function was only being called once, so I began looking for ways to nix that function and do the job more directly. Part of the challenge was that this comparison I needed to do was part of an existing
ifstatement, and it had to remain so due to the way the rest of the code worked. In short, the comparison went like this (in pseudo-Python):
thisList = ["list", "of", "unknown", "length"]
if not ":" in line and not anythingInThisList in line:
do other stuff
The first thing I tried to figure out was a way to get a single
Falseout of a list of various Boolean values. On a whim, I tried to use the built-in
maxfunction. I'd used
maxbefore in standard numeric comparisons, but never with Booleans. Not surprisingly, it worked quite well.
>>> max(True, False)
>>> max([False, False, True, False])
Getting the necessary Boolean list to pass to
maxwas a no-brainer thanks to Python's list comprehensions. Here's an example that quickly shows if a given word contains any vowels:
>>> vowels = ["a", "e", "i", "o", "u"]
>>> max([v in "sadness" for v in vowels])
>>> max([v in "shhhh" for v in vowels])
This process alone solved most of my problem. However, I couldn't always be sure that my list of things to look for would contain anything. The variable containing the list was set by an argument in a function definition, and that argument defaulted to
Noneif no other data came through. That meant that I had to somehow force the output of the
Falseif the variable was
None, or let it do its thing if not.
That got my thoughts going toward ternary operators. For those who don't know, many programming languages offer a condensed one-line shortcut for the standard
if-elsecomparison, called a ternary operator. In short, it turns this:
statement ? doThis : doThat
Not long ago I dug around to see if Python supported anything like this, and sure enough it does (as of one of the more recent versions, but I forget which one), although it isn't in the standard documentation from what I could see. At any rate, the above example would look like this using Python's approach to the ternary operator:
doThis if statement else doThat
With that applied to my
maxcomparison, the end result looked something like this (and I hope this all ends up on a single line in the blog post):
# "line" is the line of text
# "y" is either None or a list of items to find in "line"
max([x in line for x in y] if y is not None else [False])
The reason that
Falseneeds to be encapsulated inside a list is because
maxneeds a list of items through which to iterate, even if that list only contains a single item.
It's also possible to generate the Boolean list using the
mapfunction and a lambda, and the code isn't that much longer:
max(map(lambda x:x in line, y) if y is not None else [False])