3

I am trying to remove the occurrences of the vowels in the string, except if they are the starting of a word. So for example an input like "The boy is about to win" should ouput Th by is abt t wn.Here is what I have so far. Any help would be appreciated!

def short(s):
vowels = ('a', 'e', 'i', 'o', 'u')
noVowel= s
toLower = s.lower()
for i in toLower.split():
    if i[0] not in vowels:
        noVowel = noVowel.replace(i, '')        
return noVowel
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
sm15
  • 85
  • 3
  • 11

6 Answers6

6

One approach is to use a regular expression that replaces vowels not preceded by a word boundary. Also, you might want to think about some more interesting test cases if your code is supposed to handle arbitrary text with various types of punctuation.

import re
s = "The boy is about to win (or draw). Give him a trophy to boost his self-esteem."
rgx = re.compile(r'\B[aeiou]', re.IGNORECASE)
print rgx.sub('', s)  # Th by is abt t wn (or drw). Gv hm a trphy t bst hs slf-estm.
FMc
  • 41,963
  • 13
  • 79
  • 132
1

Try:

>>> s = "The boy is about to win"
>>> ''.join(c for i, c in enumerate(s) if not (c in 'aeiou' and i>1 and s[i-1].isalpha()))
'Th by is abt t wn'

How it works:

The key part of the above if the generator:

c for i, c in enumerate(s) if not (c in 'aeiou' and i>1 and s[i-1].isalpha())

The key part of the generator is the condition:

if not (c in 'aeiou' and i>1 and s[i-1].isalpha())

This means that all letters in s are included unless they are vowels that are not either (a) at the beginning of s and hence at the beginning of a word, or (b) preceded by a non-letter which would also mean that they were at the beginning of a word.

Rewritten as for loop

def short(s):
    new = ''
    prior = ''
    for c in s:
        if not (c in 'aeiou' and prior.isalpha()):
            new += c
        prior = c
    return new
John1024
  • 109,961
  • 14
  • 137
  • 171
  • this works great! but i haven't much of this? Is there a way to put this into a form including fors and ifs like i did? – sm15 Feb 11 '15 at 03:42
  • This doesn't handle situations when words starting with vowels are not preceded by spaces: ``s = "The boy is about to win (or draw)"``. That might not be an issue for the OP's data. – FMc Feb 11 '15 at 03:47
  • @sm15 I added a looping version. – John1024 Feb 11 '15 at 03:49
  • @FMc Good point. I revised the answer to define a word better. – John1024 Feb 11 '15 at 03:53
0

You can use regex on the rest of the string (ignoring the first character):

import re
s = 'The boy is about to win'
s = s[0] + re.sub(r'[aeiou]', '', s[1:])
print s # Th by s bt t wn
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

Use a regular expression:

import re

re.sub("(?<!\b)[aeiouAEIOU]", '', s)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

Through re.sub.

>>> import re
>>> s = "The boy is about to win"
>>> re.sub(r'(?i)(?<=\S)[aeiou]', r'', s)
'Th by is abt t wn'

\S matches any non-space character.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
>>> re.sub('(?<=\w)[aeiou]','',"The boy is about to win")
'Th by is abt t wn'
John Hua
  • 1,400
  • 9
  • 15