reduce(lambda x,y:x*y, [word.isalpha() for word in words])How else would you determine whether each of the words in a list uses only characters from the alphabet?
Due to constraints at work, my Python knowledge is often deeply based off of version 2.3. Now that I'm hacking on python 2.5 in my spare time, it's high time I ought to use some of the new functions available; namely, any and all. Guido makes a decent case for replacing reduce with these functions since the only easy to understand reduce statements boil down to associative operations like my boolean multiplication above. We can now rewrite my reduce as:
all([word.isalpha() for word in words])So it's even shorter and reads very nicely. And it only took me four years to catch up.
0 comments:
Post a Comment