Anti-vowel function


#1

Link to section

So I’m sure there is a less wordy way to code this function, but the way I’ve done it throws an error I don’t know the cause of.

My plan was to take each character in a string, separate them in a list, and then loop through the list. If one of the list indexes matched a vowel, I’d have it removed from the list. Then, I’d join the list indexes back together to reform the string and return it.

I understand that .remove() only removes the first index that would match, and that’s why I did a loop through the string as a list. I figured each time it reached a new index, it would be a new first time it could match and would therefore have no issues removing multiples of the same letter. Currently, my code returns:

Hy lk Words!

instead of

Hy lk Wrds!

And I see the issue is that it isn’t removing the third “o” in the string. It doesn’t seem to me like it is just an issue with not removing more than the first match since it got rid of the two “o” characters in “look”. What am I missing here that is causing just the third o to not be removed?


def anti_vowel(text):
    text = str(text)
    text = list(text)                  # turns text in list of letters
    for letter in text:                # loops through the list of letters and if letter matches vowel, remove it
        if letter == "a":
            text.remove("a")
        elif letter == "e":
            text.remove("e")
        elif letter == "i":
            text.remove("i")
        elif letter == "o":
            text.remove("o")
        elif letter == "u":
            text.remove("u")
        elif letter == "A":
            text.remove("A")
        elif letter == "E":
            text.remove("E")
        elif letter == "I":
            text.remove("I")
        elif letter == "O":
            text.remove("O")
        elif letter == "U":
            text.remove("U")

    joiner = ""                     # will be what is in between joined list indexes
    print joiner.join(text)          # debug ... joins list index letters 
    return joiner.join(text)


anti_vowel("Hey look Words!")



#2

That’s what you’d do with the vowels as well to avoid that repeated code. Also, strings can be iterated through, so obtaining a list first isn’t needed. (In fact, that’s how the list is created. By iterating through the string)

This has to do with modifying a value while at the same time reading from it. This isn’t by itself a problem, but that’ll make interactions likely, and you ran into one such interaction. It’s better to modify a separate value.

Specifically the iterator keeps increasing the index by 1, but when removing you’d want it to repeat the same index.

Is also really inefficient, since it iterates over the whole thing (first to find a matching value, and then to move the other values over to cover the gap created by the removed element)
You wouldn’t do it this way manually. You’d process one character at a time, either keeping or tossing it away, and then going to the next one.


#3

Thanks for the advice and help.

def anti_vowel(text):
    text = str(text)
    vowel = "aAeEiIoOuU"
    new = []
    for c in text:
        if c not in vowel:
            new.append(c)

    return "".join(new)
    print "".join(new)

anti_vowel("Hey look Words!")

This is what I’ve ended up going with. I think it is a bit better than the other code haha.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.