Python List Comprehension Personal Challenge
Question: > Given a text file, "words.txt", use list comprehension to read in all of the
> words in the file, and find all the words that contain at least 2 vowels.

So, I have a text file:

    
    
    The quick brown fox jumps over the lazy dog
    

And, the best attempt at getting all the words, and all the words with two or
more vowels is:

    
    
    #This could be hardcoded in, but for the sake of simplicity (as simple as simplicity gets)
    vowels = ["a","e","i","o","u"]
    filename = "words.txt"
    words = [word for word in open(filename, "r").read().split()]
    multivowels = [each for each in open(filename, "r").read().split() if sum(letter in vowels for letter in each) >= 2]
    

The output should mimic:

    
    
    All words in the file:  ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
    The words in the file that contain 2 or more vowels: ['quick', 'over']
    

My attempt to put this into one line was just to print the list comprehension
side of "words" and "multivowels" as well as the "All words in the file; "...
etc.

Is there anyone out there for the challenge of combining these two list
comprehensions into one? My teammate and I are stumped, but would love to show
it off to our professor!

Again, my final, single-line code is:

    
    
    print "All words in the file: " + str([word for word in open(filename, "r").read().split()]) + "\nAll words with more than 2 vowels: " + str([each for each in open(filename, "r").read().split() if sum(letter in vowels for letter in each) >= 2])
    

EDIT: My attempt at getting all words in the file, as well as all the words
with two or more vowels.

    
    
    vowels = ["a", "e", "i", "o", "u"]
    filename = "words.txt"
    print [(word, each) for word in open(filename, "r").read().split() if sum([1 for each in word if each in vowels]) >= 2]
    


Answer: There are some corner cases to deal with here, but if you assume a simple text
file:

    
    
    import re
    vowels = "a","e","i","o","u"
    
    answer = [[word for word in re.sub("[^\w]", " ",  sentence).split() if (sum(1 for letter in word if letter in vowels)>=2)] for sentence in open(filename,"r").readlines()]
    

