user1698174
user1698174

Reputation: 499

Finding first occurrence of a character in a word

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

Answers (3)

Rohit Jain
Rohit Jain

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

DavW
DavW

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

David Robinson
David Robinson

Reputation: 78600

and word.index('e') >= len(word)/2

Upvotes: 1

Related Questions