remove vowels in word with conditional - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: remove vowels in word with conditional (/thread-33519.html) Pages:
1
2
|
RE: remove vowels in word with conditional - ambrozote - May-02-2021 (May-01-2021, 06:04 PM)supuflounder Wrote: Don't you mean "<= 4" in line 5? 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. RE: remove vowels in word with conditional - deanhystad - May-02-2021 For a limited grammar you may get better results using straight substitution. Create a dictionary mapping your common words to their preferred abbreviation. RE: remove vowels in word with conditional - perfringo - May-02-2021 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 |