Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace vowels
#1
hello, i have a list with some names, and i have to replace wovels by " - "
here is what i tried:



list1 = ['john', 'tony', 'luck]


def replace_vowel(mlist):
            vowels = "aeiouy"
            novowel = ""
            for char in mlist:
                if char in vowels:
                    novowel = novowel.replace(char, '-')
                    return mlist

print replace_vowel(list1)
actually , the output is " None "
It has to be:

Output:
['j-hn', 't-n-', 'l-ck']
any idea? thanks !
Reply
#2
You are looping over list1, which is 'john', 'tony', and 'luck'. None of those are in vowels, so your if statement never triggers.

You need two loops. First loop over the words in mlist, which effectively what you are doing now. Then loop over the characters in vowels. Then replace that character with a hypen (-, not an underscore _) in the word. You will need to build a list of the modified words and return that after both loops are finished.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
thanks for your answer,
i now tried that:



list1 = ['john', 'tony', 'luck]

def replace_vowel(mlist):
    vowels = "aeiouy"
    new_word = []
    for letter in mlist:
        if letter in vowels:
            new_word.append('-')
    else:
        new_word.append(letter)
    return new_word

print replace_vowel(list1)
but this just prints the list:

Output:
['john', 'tony', 'luck]
Reply
#4
You create a new list (named new_word), but then return whatever you were originally passed.  So it doesn't matter what transformations you do, as they all get ignored anyway.

But even if you did return the new list, you still wouldn't see any transforms, since you iterate over words, never letters.
Reply
#5
That is not really what I suggested. I suggested two loops, one of the words in the list, one over the letters in vowels. I also suggested using replace to replace the vowels, and only appending to new_list after the loop over vowels is done (actually, I may have misstated that last part, but that's what it should be). Look at these lines:

for letter in mlist:
    if letter in vowels:
mlist is a list of words (names, really). So why would you call the loop variable 'letter'? And why would a whole name be in vowels?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
is there a way to do it like this ? since i want EVERY vowels to be replaced by '-'

list1 = ['john', 'tony', 'luck', 'o']
 
def replace_vowel(mlist):
    vowels = 'aeiouAEIOU'
    newlist = [c.replace(vowels, '-') for c in mlist]
    return newlist

 
print replace_vowel(list1)
Output:
['john', 'tony', 'luck', '-']
or with:
     newlist = ['-' if c in 'aeiou' else c for c in mlist]
     return newlist
same output
Reply
#7
list1 = ['john', 'tony', 'luck', 'o', 'mIssissippi']
  
def replace_vowel(mlist):
    vowels = 'aeiou'
    newlist = []
    for word in mlist:
        for char in word:
            if char.lower() in vowels:
                word = word.replace(char, '-')
        newlist.append(word)
    #newlist = [c.replace(vowels, '-') for c in mlist]
    return newlist
 
print(replace_vowel(list1))
Output:
['j-hn', 't-ny', 'l-ck', '-', 'm-ss-ss-pp-']
Recommended Tutorials:
Reply
#8
thanks!
Reply
#9
or
def replace_vowel(word):
    vowels = 'aeiouAEIOU'
    for vowel in vowels:
        word = word.replace(vowel,'-')
    return word

list1 = ['john', 'tony', 'luck', 'o', 'mIssissippi']
print(map(replace_vowel, list1))
Output:
['j-hn', 't-ny', 'l-ck', '-', 'm-ss-ss-pp-']
Reply
#10
Puny mortals, and iterating over characters in a word... (ie, if we're solving homework, at least give an answer that looks like they didn't do it themselves ;)
>>> def replace_vowel(words):
...   replacements = str.maketrans({ch:"-" for ch in "aeiouAEIOU"})
...   for word in words:
...     yield word.translate(replacements)
...
>>> items = ['john', 'tony', 'luck', 'o', 'mIssissippi']
>>> list(replace_vowel(items))
['j-hn', 't-ny', 'l-ck', '-', 'm-ss-ss-pp-']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove vowels in word with conditional ambrozote 12 4,005 May-02-2021, 06:57 PM
Last Post: perfringo
  counting vowels in a string project_science 3 2,510 Dec-30-2020, 06:44 PM
Last Post: buran
  Counting vowels in a string (PyBite #106) Drone4four 4 2,205 Jul-07-2020, 05:29 AM
Last Post: theknowshares
  Search & Replace - Newlines Added After Replace dj99 3 3,356 Jul-22-2018, 01:42 PM
Last Post: buran
  no vowels function alex_bgtv 6 4,980 Jan-01-2018, 08:48 PM
Last Post: alex_bgtv
  Vowels and Consonants detector OmarSinno 5 9,707 Sep-21-2017, 02:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020