Trying to remove vowels from user input with function [duplicate] - python

I'm pretty sure my code is correct but it doesn't seem to returning the expected output:
input anti_vowel("Hey look words") --> outputs: "Hey lk wrds".
Apparently it's not working on the 'e', can anyone explain why?
def anti_vowel(c):
newstr = ""
vowels = ('a', 'e', 'i', 'o', 'u')
for x in c.lower():
if x in vowels:
newstr = c.replace(x, "")
return newstr

The function str.replace(old, new[, max]) don't changes c string itself (wrt to c you calls) just returns a new string which the occurrences of old have been replaced with new. So newstr just contains a string replaced by last vowel in c string that is the o and hence you are getting "Hey lk wrds" that is same as "Hey look words".replace('o', '').
I think you can simply write anti_vowel(c) as:
''.join([l for l in c if l not in vowels]);
What I am doing is iterating over string and if a letter is not a vowel then only include it into list(filters). After filtering I join back list as a string.

Why don't you do it with regexp? According to the documentation, something like this should work:
import re
def anti_vowel(s):
result = re.sub(r'[AEIOU]', '', s, flags=re.IGNORECASE)
return result
If you're using the function often, you could compile the regexp and use the compiled version.

Try String.translate.
>>> "Hey look words".translate(None, 'aeiouAEIOU')
'Hy lk wrds'
string.translate(s, table[, deletechars])
Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.
https://docs.python.org/2/library/string.html#string.Template.substitute
Or if you're using the newfangled Python 3:
>>> table = str.maketrans(dict.fromkeys('aeiouAEIOU'))
>>> "Hey look words.translate(table)
'Hy lk wrds'

Another option is to forego the vowel variable and put the char's to remove in the loop.
def anti_vowel(text):
for i in "aeiouAEIOU":
text = text.replace(i,"")
return text
print anti_vowel("HappIEAOy")

You should do this:
initialize newstr to c, and then
for x in c.lower():
if x in vowels:
newstr = newstr.replace(x, "")
That's because str.replace(old, new[, max]) returns the a copy of the string after replacing the characters:
The method replace() returns a copy of the string in which the
occurrences of old have been replaced with new, optionally restricting
the number of replacements to max.
So, this is the correct code:
def anti_vowel(c):
newstr = c
vowels = ('a', 'e', 'i', 'o', 'u')
for x in c.lower():
if x in vowels:
newstr = newstr.replace(x,"")
return newstr
You can also do it in a more pythonic way:
''.join([x for x in c if x not in vowels])

vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'I', 'E', 'O', 'U')
for char in text:
if char in vowels:
text = text.replace(char, '')
return text

One more simpler way can be extracting the non-vowel characters from string and returning them.
def anti_vowel(text):
newstring=""
for i in text:
if i not in "aeiouAEIOU":
newstring=newstring+i
text=newstring
return text

I know there are many correct solutions on this subject but I thought to add few fun ways of solving this problem.
If you come from a C++/C# or Java, you will tend to use something like compare then action using the index to remove the unwanted entry in a for loop. Python has the Remove and Del functions. Remove function uses the value and
del uses the index.The pythonic solution is in the last function. Lets see how we can do that:
Here we are using the index in a for loop and del function very similar in C++:
def remove_vol(str1):
#list2 = list1 # this won't work bc list1 is the same as list2 meaning same container#
list1 = list(str1)
list2 = list(str1)
for i in range(len(list1)):
if list1[i] in volwes:
vol = list1[i]
x = list2.index(vol)
del list2[x]
print(list2)
Using the remove function:
def remove_vol(str1):
list1 = list(str1)
list2 = list(str1)
for i in list1:
if i in volwes:
list2.remove(i)
print(list2)
Building new string that does not contain the unwanted chars using their indexes:
def remove_vol(str1):
list1 = list(str1)
clean_str = ''
for i in range(len(list1)):
if list1[i] not in volwes:
clean_str += ''.join(list1[i])
print(clean_str)
Same as in the solution in above but using the value:
def remove_vol(str1):
list1 = list(str1)
clean_str = ''
for i in list1:
if i not in volwes:
clean_str += ''.join(i)
print(clean_str)
How you should do it in python? Using list comprehension! It is beautiful:
def remove_vol(list1):
clean_str = ''.join([x for x in list1 if x.lower() not in volwes])
print(clean_str)

def anti_vowel(text):
new=[]
vowels = ("aeiouAEIOU")
for i in text:
if i not in vowels:
new.append(i)
return ''.join(new)
i hope this helps..

def anti_vowel(text):
new_text = ""
for i in text:
if i == 'a' or i == 'A':
pass
elif i == 'e' or i == 'E':
pass
elif i == 'I' or i == 'i':
pass
elif i == 'o' or i == 'O':
pass
elif i == 'u' or i == 'U':
pass
else:
new_text = new_text + i
return new_text
print anti_vowel('Hey look Words!')

My implementation:
# Ask the user for input:
user_input = input("enter a string with some vowels: ")
print("input string: " + str(user_input))
vowels = ('a','e','i','o','u','A','E','I','O','U')
new_string="";
for i in range(0,len(user_input),1):
if user_input[i] in vowels:
print ('found a vowel, removing...')
else:
new_string+=user_input[i]
print("I've removed the vowels for you. You're welcome! The new string is: " + new_string)

A fairly simple approach could be;
def anti_vowel(text):
t = ''
for c in text:
if c in "aeiouAEIOU":
pass
else:
t += c
return t

def anti_vowel(text):
t=""
for c in text:
for i in "ieaouIEAOU":
if c==i:
c=""
else:
c=c
t=t+c
return t

Related

Python function with list without any spaces

Define a function convert(input str) to return a list of characters based on the input
string input str. Specifically, each character in input str will be included as a separate element of
the returned list, while any spaces in input str will be ignored
def convert(input_str):
newlist = []
reallist = [char for char in input_str]
for k in input_str:
if k:
newlist.append(k)
return newlist
print(convert("Hi You"))
this gives output
['H', 'i', ' ', 'Y', 'o', 'u']
but I do not want the empty space between i and y
Instead of using a list comprehension and then filter the resulting list, you can do it in one step using if in the list comprehension:
def convert(input_str: str):
return [c for c in input_str if not c.isspace()]
print(convert("Hi You")) # ['H', 'i', 'Y', 'o', 'u']
Instead of using isspace, you could use [c for c in input_str if c != ' '] instead (although I believe using isspace is generally recommended).
you just need to add an if statement before you append an item to the list, only if char != " ": append char to new_list
def convert(input_str):
new_list = []
for char in input_str:
if char != " ": # <-----
new_list.append(char)
return new_list
print(convert("Hi You"))
def convert(input_str):
newstr = "".join(input_str.split())
reallist = [char for char in newstr]
return reallist
print(convert("Hi You"))
Maybe this if it is not just one space you are trying to skip
Python has built-in method for string that replaces string inside of string with another string. It takes the character that you want to replace and with what you want to replace it with as arguments.
def convert(input_str):
# Replace whitespaces with nothing
input_str = input_str.replace(" ", "")
new_list = [char for char in input_str]
return new_list
print(convert("Hi You"))

list.remove(x): x not in list not understood [duplicate]

This question already has answers here:
Correct code to remove the vowels from a string in Python
(13 answers)
Closed 4 years ago.
I'm beginning to learn Python and I'm trying to create a function that removes vowels from a given string.
This is my Code :
def anti_vowel(text):
li = []
for l in text:
li.append(l)
while 'a' in li:
li.remove('a')
while 'A' in li:
li.remove('A')
while 'A' in li:
li.remove('e')
while 'e' in li:
li.remove('E')
while 'E' in li:
li.remove('i')
while 'i' in li:
li.remove('I')
while 'I' in li:
li.remove('o')
while 'o' in li:
li.remove('O')
while 'u' in li:
li.remove('u')
while 'U' in li:
li.remove('U')
return "".join(li)
I get the "list.remove(x): x not in list" error when I try to run it.
I know this error's been already asked about here but I didn't really get the answers in those specific cases.
thanks for reading and please help :)
def anti_vowel(text):
li = ''
for l in text:
if l.lower() not in 'aeiou':
li+=l
return li
You are overcomplicating it, just use a generator expresion:
def anti_vowel(text):
return "".join(x for x in text if x not in "AEIOUaeiou")
>>> anti_vowel("asldkfoihyoihbiw")
'sldkfhyhbw'
You can use loops also:
def anti_vowel(text):
li = []
for l in text:
if l not in "AEIOUaeiou":
li.append(li)
return "".join(li)
You have some mis-matches in your while statement, for example:
while 'e' in li:
li.remove('E')
If there is no 'E' but there is an 'e' this will cause a problem.
You either need to go through and make sure they are consistent.
Or you could write a small function to deal with each vowel:
def remove(letters, vowel):
while vowel in letters:
letters.remove(vowel)
You can then call this for each vowel.
def anti_vowel(text):
li = []
for l in text:
li.append(l)
for vowel in ['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U']:
remove(li, vowel)
return "".join(li)
A more Pythonic way would be to pull out the required letters, using a list comprehension or generator, as per the answer from Netware. I'm just pointing out the source of your error.
If you catch yourself repeating something lots, and copy/pasting then tweaking, you can easily miss a few places that need tweaking.
Try to change the repeats into a function.

Finding a letter in a list

I have a problem that states: Write a function that takes, as an argument, a list and a string,and returns a Boolean based on whether or not all of the letters in the string appear somewhere in the list. Here is what I have so far.
def findLetters(myList, myString):
for letter in myString:
if letter in myList:
return True
return False
Here's a basic solution that's closer to what you've started:
def findLetters(myList, myString):
found_all = False
for s in myString: # check each letter in the string
if s in myList: # see if it is in the list
found_all = True # keep going if found
else:
found_all = False # otherwise set `found` to False
break # and break out of the loop
return found_all # return the result
result = findLetters(['a', 'l', 'i', 's', 't'], 'mlist')
# 'm' is not in the list
print result # False
# all letters in the string are in the list;
# ignores any extra characters in the list that are not in the string
result = findLetters(['a', 'l', 'i', 's', 't', 'p'], 'alist')
print result # True
You can map all letters using a lambda which creates a list of boolean values for all letters in my_string.
The function all returns true if all values in the l list ar True.
def find_letters(my_list, my_string):
l = map(lambda x: x in my_list, my_string)
return all(l)
print(find_letters(['a', 'b', 'c'], 'cab'))
you are returning True if any letter match in myString, not all letter in myString.
You could do the other way around, if any letter not match in myString, return False
def findLetters(myList, myString):
for letter in myString:
if letter not in myList:
return False
return True
OR use builtin function all
def findLetters(myList, myString):
return all(letter in myList for letter in myString)

Correct code to remove the vowels from a string in Python

I'm pretty sure my code is correct but it doesn't seem to returning the expected output:
input anti_vowel("Hey look words") --> outputs: "Hey lk wrds".
Apparently it's not working on the 'e', can anyone explain why?
def anti_vowel(c):
newstr = ""
vowels = ('a', 'e', 'i', 'o', 'u')
for x in c.lower():
if x in vowels:
newstr = c.replace(x, "")
return newstr
The function str.replace(old, new[, max]) don't changes c string itself (wrt to c you calls) just returns a new string which the occurrences of old have been replaced with new. So newstr just contains a string replaced by last vowel in c string that is the o and hence you are getting "Hey lk wrds" that is same as "Hey look words".replace('o', '').
I think you can simply write anti_vowel(c) as:
''.join([l for l in c if l not in vowels]);
What I am doing is iterating over string and if a letter is not a vowel then only include it into list(filters). After filtering I join back list as a string.
Why don't you do it with regexp? According to the documentation, something like this should work:
import re
def anti_vowel(s):
result = re.sub(r'[AEIOU]', '', s, flags=re.IGNORECASE)
return result
If you're using the function often, you could compile the regexp and use the compiled version.
Try String.translate.
>>> "Hey look words".translate(None, 'aeiouAEIOU')
'Hy lk wrds'
string.translate(s, table[, deletechars])
Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.
https://docs.python.org/2/library/string.html#string.Template.substitute
Or if you're using the newfangled Python 3:
>>> table = str.maketrans(dict.fromkeys('aeiouAEIOU'))
>>> "Hey look words.translate(table)
'Hy lk wrds'
Another option is to forego the vowel variable and put the char's to remove in the loop.
def anti_vowel(text):
for i in "aeiouAEIOU":
text = text.replace(i,"")
return text
print anti_vowel("HappIEAOy")
You should do this:
initialize newstr to c, and then
for x in c.lower():
if x in vowels:
newstr = newstr.replace(x, "")
That's because str.replace(old, new[, max]) returns the a copy of the string after replacing the characters:
The method replace() returns a copy of the string in which the
occurrences of old have been replaced with new, optionally restricting
the number of replacements to max.
So, this is the correct code:
def anti_vowel(c):
newstr = c
vowels = ('a', 'e', 'i', 'o', 'u')
for x in c.lower():
if x in vowels:
newstr = newstr.replace(x,"")
return newstr
You can also do it in a more pythonic way:
''.join([x for x in c if x not in vowels])
vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'I', 'E', 'O', 'U')
for char in text:
if char in vowels:
text = text.replace(char, '')
return text
One more simpler way can be extracting the non-vowel characters from string and returning them.
def anti_vowel(text):
newstring=""
for i in text:
if i not in "aeiouAEIOU":
newstring=newstring+i
text=newstring
return text
I know there are many correct solutions on this subject but I thought to add few fun ways of solving this problem.
If you come from a C++/C# or Java, you will tend to use something like compare then action using the index to remove the unwanted entry in a for loop. Python has the Remove and Del functions. Remove function uses the value and
del uses the index.The pythonic solution is in the last function. Lets see how we can do that:
Here we are using the index in a for loop and del function very similar in C++:
def remove_vol(str1):
#list2 = list1 # this won't work bc list1 is the same as list2 meaning same container#
list1 = list(str1)
list2 = list(str1)
for i in range(len(list1)):
if list1[i] in volwes:
vol = list1[i]
x = list2.index(vol)
del list2[x]
print(list2)
Using the remove function:
def remove_vol(str1):
list1 = list(str1)
list2 = list(str1)
for i in list1:
if i in volwes:
list2.remove(i)
print(list2)
Building new string that does not contain the unwanted chars using their indexes:
def remove_vol(str1):
list1 = list(str1)
clean_str = ''
for i in range(len(list1)):
if list1[i] not in volwes:
clean_str += ''.join(list1[i])
print(clean_str)
Same as in the solution in above but using the value:
def remove_vol(str1):
list1 = list(str1)
clean_str = ''
for i in list1:
if i not in volwes:
clean_str += ''.join(i)
print(clean_str)
How you should do it in python? Using list comprehension! It is beautiful:
def remove_vol(list1):
clean_str = ''.join([x for x in list1 if x.lower() not in volwes])
print(clean_str)
def anti_vowel(text):
new=[]
vowels = ("aeiouAEIOU")
for i in text:
if i not in vowels:
new.append(i)
return ''.join(new)
i hope this helps..
def anti_vowel(text):
new_text = ""
for i in text:
if i == 'a' or i == 'A':
pass
elif i == 'e' or i == 'E':
pass
elif i == 'I' or i == 'i':
pass
elif i == 'o' or i == 'O':
pass
elif i == 'u' or i == 'U':
pass
else:
new_text = new_text + i
return new_text
print anti_vowel('Hey look Words!')
My implementation:
# Ask the user for input:
user_input = input("enter a string with some vowels: ")
print("input string: " + str(user_input))
vowels = ('a','e','i','o','u','A','E','I','O','U')
new_string="";
for i in range(0,len(user_input),1):
if user_input[i] in vowels:
print ('found a vowel, removing...')
else:
new_string+=user_input[i]
print("I've removed the vowels for you. You're welcome! The new string is: " + new_string)
A fairly simple approach could be;
def anti_vowel(text):
t = ''
for c in text:
if c in "aeiouAEIOU":
pass
else:
t += c
return t
def anti_vowel(text):
t=""
for c in text:
for i in "ieaouIEAOU":
if c==i:
c=""
else:
c=c
t=t+c
return t

use .replace() on only one character of a string with repetitive characters

I have this hangman problem and in this part, I'm trying to replace the placeholder '_ ' with the character that they've correctly guessed. e.g. if secretWord = 'lettuce', and they've guessed e, I want it to go through secretWord and check if e is in it, and then replace that '_ ' with e, so it returns
_ e_ _ _ _ e
my code goes:
create a placeholder string representation of secretWord
so Far = ''
for char in secretWord:
soFar = soFar + '_ '
check/replace the placeholder with the correct character
for char in secretWord:
if char in lettersGuessed:
soFar.replace('_ ', str(char))
print soFar
But in pythontutor.com, where secretWord = 'lettuce' and lettersGuessed = ['z', 'x', 'q', 'l', 'e', 't', 't', 'u', 'c', 'e'] my output was:
_ _ _ _ _ _ _
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
So basically, it saw that l was in letters guessed, and since it is supposed to replace '_ ' with that character, it replaced all of them. But I just want it to do it that once (and again, if it is in secretWord again). And then it couldn't replace any of the other characters because none of them were '_ '....
You don't want to use replace when you're trying to replace a character at a certain position; it's only for when you want to replace all characters, at all indexes, that match a certain value.
The way to replace a character at an index is with slicing:
soFar = soFar[:index] + newChar + soFar[index+1:]
In this case, each character in secretWord matches two characters in soFar, so you have to double those:
soFar = soFar[:index*2] + newChar + soFar[index*2+1:]
But how do you know the index? Not by calling index on the string—that will have the same problem, returning the index of the first character with that value, not the one you want. Use the enumerate function to keep count for you as you go along:
for index, char in enumerate(secretWord):
As kevinsa5 suggests, if you'd used a list instead of a string, you would make this simpler:
soFar = []
for char in secretWord:
soFar.append('_')
Now, the replacement is just:
soFar[index] = char
However, if you print that out as a string, it'll look like ['_', '_'] rather than _ _. To turn it into a nice string, you need to use the join method:
print ' '.join(soFar)
Use a list, rather than a string:
soFar = [char if char in lettersGuessed else '_' for char in secretWord]
print(' '.join(soFar))
Also, it would be better to have lettersGuessed be a set rather than a list. Since you care only about membership in the set, and it is appropriate for lettersGuessed to only contain unique items.
The way it is now, you are replacing every single '_' with the first guessed letter that appears in the secretWord. So, when the guessed l, it filled in every _ with l.
What you can do instead:
for index, char in enumerate(secretWord):
if char in lettersGuessed:
soFar = soFar[:index-1]+char+soFar[index+1:]
print soFar
Though abarnert has given you a neat solution already,
I approached this problem by tracking "unguessed_letters" and using "maketrans"
Solution:
from string import maketrans
def process_guess(secret_word, unguessed_letters, guess):
if guess in secret_word:
unguessed_letters = unguessed_letters.replace(guess, '')
trantab = maketrans(unguessed_letters, "_"*len(unguessed_letters))
return secret_word.translate(trantab), unguessed_letters
Usage Demonstration:
secret_word = "lettuce"
unguessed_letters = secret_word
lettersGuessed = ['z', 'x', 'q', 'l', 'e', 't', 't', 'u', 'c', 'e']
for i in lettersGuessed:
secret_mask, unguessed_letters = process_guess(secret_word, unguessed_letters, i)
print secret_mask
Execution Output:
$ python j.py
_______
_______
_______
l______
le____e
lett__e
lett__e
lettu_e
lettuce
lettuce

Categories