Reputation: 499
what I am trying to do: By filtering the lowers list, create and print a list of the words which satisfy all of the following criteria:
the word is at least 6 characters long; the word contains the letter 'e' at least 3 times; the first occurrence of the letter 'e' is in the second half of the word.
I have this so far: was used earlier:
words = [ line.strip() for line in file(DATA+'english_wordlist.txt') ]
(lowers was defined earlier in my work as a partial set of words)
[word for word in lowers if len(word)>=6 and word.count('e')>=3 and 'e' is after word(len(word)/2::)]
I know that 'e' is after word(len(word)/2::) is wrong but this just my rough logic. How exactly would i accomplish this?
Upvotes: 1
Views: 914
Reputation: 213223
[word for word in lowers if len(word)>=6 and word.count('e')>=3 and
'e' not in word[int(len(word)/2) + 1:]]
len(word)/2
is true division
in Python.. So it may give float value..
So, typecast it to int
and add 1 to move it after middle..
*EDIT: - Or you can simply use len(word)//2
(which is floor division
) and doesn't need to be typecasted..
So, here's is an alternative: -
[word for word in lowers if len(word)>=6 and word.count('e')>=3 and
'e' not in word[(len(word)//2) + 1:]]
Upvotes: 0
Reputation: 886
Checking that 'e' occurs first in the second half of the word is equivalent to knowing that it does not occur in the first half of the word. That should help you.
Upvotes: 0