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
#11
(May-01-2021, 06:04 PM)supuflounder Wrote: 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.

Thank you for your explanation on what words[index] is doing. It makes sense to me now.
This is not for a homework. I am working on a script for using at work so shorten some element naming. Very new to Python so trying my best to learn as I go with new challenges.
Reply
#12
For a limited grammar you may get better results using straight substitution. Create a dictionary mapping your common words to their preferred abbreviation.
ambrozote likes this post
Reply
#13
I would split string to words and process word with function. For replacement I would use str.translate. Assuming that this is text in english:

import string

s = "Masonry - Concrete Block (Small) - Misc Air Layer - Insulation - Aluminium"
        
def process(word):
    mapping = str.maketrans(dict.fromkeys('aeiou', ''))
    length = 0
    for char in word:
        if char in string.ascii_letters:
            length += 1
            if 4 < length:
                return word.translate(mapping)
    else:    # no-break
        return word

print(' '.join(process(word) for word in s.split()))

# -> Msnry - Cncrt Blck (Smll) - Misc Air Lyr - Insltn - Almnm
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


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,206 Jul-07-2020, 05:29 AM
Last Post: theknowshares
  Python Speech recognition, word by word AceScottie 6 15,866 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Remove a sentence if it contains a word. lokhtar 6 5,780 Feb-11-2020, 04:43 PM
Last Post: stullis
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 6,993 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 4,725 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  difference between word: and word[:] in for loop zowhair 2 3,628 Mar-03-2018, 07:24 AM
Last Post: zowhair
  no vowels function alex_bgtv 6 4,983 Jan-01-2018, 08:48 PM
Last Post: alex_bgtv
  replace vowels niru 9 20,616 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