Anti_vowel


#1
def anti_vowel(text):
  vowel = ["a", "e", "i", "o", "u","A","E","I","O","U"]
  for i in range(len(text)):
    for j in vowel:
      if text[i]=j:    // line 5
        text.pop[i]
  return text

What is wrong here? It says invalid syntax in line 5


#2

a single equal means assign, like here:

vowel = ["a", "e", "i", "o", "u","A","E","I","O","U"]

assign (well, technically python variable are named, not assigned) list to vowel variable.

Which is fine, but assigning (naming) is not something i would do in an if condition


#3

Okay, sorry. I corrected it but it seems that it still won’t work as .pop() can’t be used on string. Is there any way to remove a character at a particular index in a string?


#4

methods belong to specific data types, string have certainly methods (like .remove()) which can remove characters from a string.

I hadn’t even seen that yet, but i would absolutely not go for the .remove() approach, if you attempt it you will see what i mean.


#5

No remove method on string

@shivensingh strings are not mutable, cannot ever be changed. You can create new ones. To manipulate individual characters you might put them in a list. list isn’t very good for “removing” elements though, because that operation doesn’t operate on just the character removed but also everything that follows and perhaps also everything leading up to it. One therefore has to choose a data structure that is suitable for what is being done.

If you think about what necessarily has to happen that’ll tell you a lot about what “removing” a single character from a string involves. It involves reading up to that location, skipping over it, and reading the rest. So you’d create those two strings and then concatenate them together.

In the case of anti_vowel you probably shouldn’t “remove” one character at a time since it would involve processing the whole thing for each removal. Instead you’d iterate over characters and put the ones to keep in a list, from which you would then create a string as the last step. Note that this ends up a single pass over the string for filtering out vowels, and another for creating the final string. (which is great, 1000 characters would be 2000 steps, it could have been 1 million steps if doing 1000 steps for each character.

Manipulating text is a very common thing to do, there are lots of operations available. Some custom actions might involve putting the characters in your own data structure while you manipulate them.


#6

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