Anti_vowel, how to use a way of delete


#1

I know the method that creating a string="" first and them string=string+i if i is not in aeiou.
But I want to try a method of deleting, which means string=text and delete i if i is in aeiou, but I failed.

def anti_vowel(text):
    vowel=["a","e","i","o","u","A","E","I","O","U"]
    for i in text:
        for j in vowel:
            if i==j:
                del text[i]
    return text

This is my code. Could you help me?


#2

text is not a list, and text[i] is not a list item. There is no del method attributed to a string object.

Strings are immutable, so we cannot march in and remove a character. It's just not done. We get around this by making the string into a list. There is a del method on lists.

text = list(text)

before the loop, and in the return,

''.join(text)

Just know that you are messing with the object your loop is set to iterate. Some elements will be skipped.


#3

Thank you, but I changed the codes and tried 2 ways and unluckily neither works. Could you help?

def anti_vowel(text):
    vowel=["a","e","i","o","u","A","E","I","O","U"]
    text=list(text)
    for j in vowel:
        for i in text:
            if i==j:
                text.remove(i)
    return "".join(text)

print anti_vowel("balaanbulA")
#Result is "blanbl", why there is an "a"?
--------------------------------------------------------
def anti_vowel(text):
    vowel=["a","e","i","o","u","A","E","I","O","U"]
    text=list(text)
    for j in vowel:
        for i in text:
            if text[i]==j:
                del text[i]
    return "".join(text)
#Why this code does not work?

#5

I think the problem with this code may be in the double loop thing (for ... : for ... : ). Try reducing it to one simple loop.

I'd say that since vowels is a list of separate letters, you could try comparing items from text to the list of vowels with something like looping the items in text, checking them against the vowels list (you could use something like if x not in vowels, and then you could check out appending/removing things from lists (list.append(item)) or list.remove(item)).


#6

The suggestion by @karaeska is on the right track. However, I would not iterate the text, as much as the vowels, which can be in a string, rather than a list.

vowels = 'aeiouAEIOU'

for vowel in vowels:

To use the list.remove() method we would cycle through a while loop until all are removed.

    while vowel in text:
        text.remove(vowel)

The del() method is for removing bindings on variable, and not really applicable to this problem.


#7

Thanks. I tried new codes later, and the first code actually works.

But do you know why the second does not work?

def del_vowel(text):
    vowel="aeiouAEIOU"
    text=list(text)
    for letter in vowel:
        while letter in text:
            text.remove(letter)
    return "".join(text)

print del_vowel("AuotoIErai")
# result is "tr", which is correct.

def del_vowel(text):
    vowel="aeiouAEIOU"
    text=list(text)
    for letter in text:
        if letter in vowel:
            text.remove(letter)
    return "".join(text)
    
print del_vowel("AuotoIErai")
#not workable!!!

#8

@mtf very nice code. My code looks like:

def anti_vowel(text):
	vowel = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
	output = ""
	for i in text:
		if i in vowel:
			continue
		else:
			output += i
	return(output)
	
print(anti_vowel(raw_input("Enter sentence: ")))

#9

def del_vowel(text):
    vowel="aeiouAEIOU"
    text=list(text)
    for letter in text:
        if letter in vowel:
            text.remove(letter)
            print (text)
    return "".join(text)
    
print (del_vowel("Hey look Words!"))

['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
Hy lk Words!

Can you spot the issue? It has to do with left shift. When the first o is removed, the text to the right shifts left to fill the void. The second o is now in the interator position, but since that term is finished the iterator moves to the next one, skipping the second o. When it gets to third o, text.remove() takes out the second one, not the third.


#12