0

Here is the code I have:

import string # to import the alphabet <-'abcdefghijklmnopqrstuvwxyz'
alpha = string.lowercase
vowels = "aeiou"
alpha = alpha.strip(vowels)
sentence = "methinks it is like a weasel"
words = sentence.split(" ")
characters = list(sentence)

words
characters
alpha
vowels

when I print I get this:

['methinks', 'it', 'is', 'like', 'a', 'weasel']

['m', 'e', 't', 'h', 'i', 'n', 'k', 's', ' ', 'i', 't', ' ', 'i', 's', ' ', 'l', 'i', 'k', 'e', ' ', 'a', ' ', 'w', 'e', 'a', 's', 'e', 'l']

'bcdefghijklmnopqrstuvwxyz'
'aeiou'

I would like it to remove all vowels from the alphabet, but thus far it is only removing the a (the leading coefficient).

johnbowen
  • 121
  • 6

2 Answers2

0
[character for character in sentence if character not in vowels] # returns list

This list comprehension should do the trick for you.

The way it works is that it takes advantage of the fact that vowels and sentences are iterables: you can define a for-loop over them, so to speak. Specifically, it pulls every character in sentence, checks if that same character does not appear in vowels, and only then does it insert it into a list.

If you want a string at the end, simply use join:

''.join([character for character in sentence if character not in vowels]) # returns string without vowels
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
0

I wrote a sample code, hope it helps.

words = ['Ahmad Siavashi', 'Mohammad Siavashi']
print [''.join([c for c in w if c not in 'aeoui']) for w in words]

Output:

['Ahmd Svsh', 'Mhmmd Svsh']

and if you only want for one string, use:

print ''.join([c for c in "methinks it is like a weasel" if c not in 'aeoui'])