Python Forum
remove vowels in word with conditional
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove vowels in word with conditional
#1
Hello,

I am trying to shorten large strings by applying some rules to it but not being able to figure it out.

Here is the code I have tried so far w/o success.
The string in endString is my end goal using the startString sample.
The rules are added as comments below.

Thank you in advance for your help

startString = "Masonry - Concrete Block (Small) - Misc Air Layer - Insulation - Aluminium"
endString = "Msnr-CncrBlck(Smll)-MiscAirLyr-Insltn-Almnm"

#rules
#words => 4 chars are left untouched
#words > 4 chars have all lowercase vowels removed
#special chars are kept, such as parenthesis and hyphens

words = startString.split()

for index in range(len(words)):
    if len(words[index]) > 4:
        vowels = ('a', 'e', 'i', 'o', 'u')
        for x in words[index]:
            if x in vowels:
                shorter = words[index].replace(x, "")

endStringByCode = "".join(shorter)
print (endStringByCode)
Reply
#2
Don't you mean "<= 4" in line 5?

The problem is that you are doing
shorter = words[index].replace(x, "")
So let's look at what happens

Let words[index] be the string "I don't want any lower-case vowels".
The first time through the loop starting on line 15, your first match is the "o" in "don't". So shorter is set to
"I dn't want any lower-case vowels"
The next time there is a match it matches the a in want, so shorter is set to
"I don't wnt any lower-case vowels"
Do you see the problem?

What you want to do is always test shorter, not words[index]. Which is a bit tricky, because you have removed the letter, so you have to manage the range. I'm not going to write the code for your homework, but this is as much help as I will give.
Reply
#3
No rule about spaces but still they are removed. Why?

Also:

Masonry -> Msnr but Layer -> Lyr. Why in first case y is removed but in second not? Both words are longer than 4 chars.

Concrete -> Cncr. Why t is removed?

words => 4 chars are left untouched should probably be <= otherwise rules don't make sense.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
OK, I am trusting this is not homework, which means I may be duped.
There are a number of issues. See this code, which gives what you want:
startString = "Masonry - Concrete Block (Small) - Misc Air Layer - Insulation - Aluminium"
endString = "Msnr-CncrBlck(Smll)-MiscAirLyr-Insltn-Almnm"
 
#rules
#words => 4 chars are left untouched
#words > 4 chars have all lowercase vowels removed
#special chars are kept, such as parenthesis and hyphens
 
words = startString.split()
vowels = ('a', 'e', 'i', 'o', 'u', 'y')
out_string = ''

for word in words:
    if len(word) >= 4:
        shorter = ''
        for x in word:
            if x not in vowels:
                shorter = shorter + x
    else:
        shorter = word
        
    out_string = out_string + shorter

print (out_string)
Any time you have range(len(... there is a better way. In this case, for word in words. You don't have to mess with index at all. I moved the definition of vowels outside the loop, as doing that every time is inefficient. Rather than removing characters from the start string (btw - CamelCsae is frowned upon, better to use underscore like in out_string) I build the shorter string from acceptable characters, if the length is long enough. I added 'y' as a vowel as in your example it is to be removed. Perfringo is right, by using split you lose the spaces, but I assume that is ok?
ambrozote likes this post
Reply
#5
I'm going to put my version as well.
newwords = []
vowels = ('a', 'e', 'i', 'o', 'u')
for word in words:
    if len(word) > 4:
        for letter in word:
            if letter in vowels:
                word = word.replace(letter, '')
        newwords.append(word)
    else:
        newwords.append(word)
print(' '.join(newwords))
Output:
Msnry - Cncrt Blck (Smll) - Misc Air Lyr - Insltn - Almnm
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
(May-01-2021, 08:27 PM)jefsummers Wrote: See this code, which gives what you want

As conditions are ambiguous it's hard to say what is wanted output. For example: should (oye) -> (oye) or (oye) -> ()? Your code does latter but I personally think that former should be correct (three letter word in parentheses i.e 'untouched word').
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
By gives you what you want I meant that the output matches his example.

I am concerned about some other examples such as menator's that modify the item being iterated upon.
Reply
#8
I do not understand. I modified the item being iterated? I'm still learning. Could you explain please? Thanks.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
Menator - in line 5 you are iterating over word. In line 7 you modify word. So, let's say you are on the 6th letter of an 8 letter word, and you eliminate that letter. Python then moves to the 7th letter, which was the 8th letter, skipping the 7th.
Reply
#10
(May-01-2021, 08:27 PM)jefsummers Wrote: OK, I am trusting this is not homework, which means I may be duped.
There are a number of issues. See this code, which gives what you want:
startString = "Masonry - Concrete Block (Small) - Misc Air Layer - Insulation - Aluminium"
endString = "Msnr-CncrBlck(Smll)-MiscAirLyr-Insltn-Almnm"
 
#rules
#words => 4 chars are left untouched
#words > 4 chars have all lowercase vowels removed
#special chars are kept, such as parenthesis and hyphens
 
words = startString.split()
vowels = ('a', 'e', 'i', 'o', 'u', 'y')
out_string = ''

for word in words:
    if len(word) >= 4:
        shorter = ''
        for x in word:
            if x not in vowels:
                shorter = shorter + x
    else:
        shorter = word
        
    out_string = out_string + shorter

print (out_string)
Any time you have range(len(... there is a better way. In this case, for word in words. You don't have to mess with index at all. I moved the definition of vowels outside the loop, as doing that every time is inefficient. Rather than removing characters from the start string (btw - CamelCsae is frowned upon, better to use underscore like in out_string) I build the shorter string from acceptable characters, if the length is long enough. I added 'y' as a vowel as in your example it is to be removed. Perfringo is right, by using split you lose the spaces, but I assume that is ok?

I like this approach and I can understand your explanation.
Also, thank you for your advice on best practice.
it is also ok to loose the spaces as my goal here is to get the whole lenght of the string shorter while still keeping it human readable.

Thank you very much for your help :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  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
  Python Speech recognition, word by word AceScottie 6 15,857 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Remove a sentence if it contains a word. lokhtar 6 5,770 Feb-11-2020, 04:43 PM
Last Post: stullis
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 6,989 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 4,717 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  difference between word: and word[:] in for loop zowhair 2 3,620 Mar-03-2018, 07:24 AM
Last Post: zowhair
  no vowels function alex_bgtv 6 4,980 Jan-01-2018, 08:48 PM
Last Post: alex_bgtv
  replace vowels niru 9 20,604 Sep-26-2017, 12:46 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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