Alright here is the bit in question.
if any(char in word for char in ["a", "ā", "e", "ē", "i", "ī", "u", "ū", "o", "ai", "ei", "ui"]):
for ch in ["a", "ā", "e", "ē", "i", "ī", "u", "ū", "o", "ai", "ei", "ui"]:
if ch in word:
word = word.replace(ch, ch + "p" + ch)
This might seem a bit weird but its for a translator for Latvian umm.. slang. I know finding the vowels could probably be done much more efficiently but my problem is that i want it to not take, for example, the "i" in "ei" if the ei is there. right now it takes for example "aiza" and gives "apaipaipizapa" instead of the preferred "aipaizapa" . Hope this hasn't been asked too many times, English is not my native so i didn't know how to formulate an effective search term. Thank you in advance.
EDIT: I am afraid i worded my issue quite poorly. what i need is So, here is an example .
list = ["a", "b", "ay", "by"]
state = input("Type a, b, ay or by")
for char in list
if char in state
state = state.replace(char, k)
print state
So now, if the input is by or ay it will give kk but I need it to give out a single k . How do i achieve Python 3 ignoring the smaller value if it is a part of the bigger one?
You can join those characters to a regular expression using |
. Make sure to sort the multi-char parts first, so they are preferred in the match (ie it matches ai
instead of a
and then i
). Then use re.sub
with a callback function:
>>> chars = ["a", "ā", "e", "ē", "i", "ī", "u", "ū", "o", "ai", "ei", "ui"]
>>> s = "aiza"
>>> p = "|".join(sorted(chars, key=len, reverse=True))
>>> print(p)
ā|ē|ī|ū|ai|ei|ui|a|e|i|u|o
>>> re.sub(p, lambda m: "{0}p{0}".format(m.group()), s)
'aipaizapa'
Regular expression matches are non-overlapping, and since it replaces all the characters at once, and one one after the other, characters that are part of other characters will not be a problem.
Your code is not python3, but from what I understood from your second example, try this:
list1 = ["a", "b", "ay", "by"]
state = input("Type a, b, ay or by: ")
for char in state:
if char in list1:
print("k")
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.