[英]How to take non alpha characters out of a string and put them back into the string in the same order?
我正在尝试制作一个 Pig 拉丁语翻译器,但是我的代码有一个问题,当输入诸如“hi”或 /chair/ 之类的词时,它无法正常工作。 这是因为我需要我的代码来检测输入是否包含非字母字符,将其从字符串中取出,并在更改完字符串后将其放回原处。 不过,我正在努力做到这一点。
# Pig Latin 11/11/20
#!/usr/bin/env python3
vowels = ("A", "E", "I", "O", "U")
message = input("Input text to be translated to Pig
Latin\n")
message = message.split()
not_alpha = {}
new_message = []
for word in message:
这个注释掉的部分是我试图解决这个问题的部分,在单词通过编辑过程之前,它会通过这里并删除 non_alpha 键并将它们放在一个名为 not_alpha 的字典中。 我的想法是将它放入字典中,以字符为键,以字符串中的索引为值。 然后,最后,我将遍历单词中的每个字母,并按顺序使用所有非字母字符重建单词。
# for letter in word:
# if not letter.isalpha():
# not_alpha[letter] = word.index(letter)
# word = word
# for k in not_alpha.keys():
# word.replace(k, "")
letter_editing = word[0]
if word.isalpha():
if letter_editing.upper() in vowels:
word += "yay"
else:
letter_editing = word[0:2]
if letter_editing.upper() in vowels:
word = word[1:] + word[0] + "ay"
else:
word = word[2:] + word[0:2] + "ay"
# for letter in word:
# if word.index(letter) in not_alpha.values():
虽然我对您需要应用的所有猪拉丁规则并不肯定,但从我看来,您只应用了两个:
鉴于这 2 条规则和以下观察结果:
您可以按如下方式完成翻译:
from string import whitespace, punctuation
vowels = 'aeiou'
message = "Oh! my what a beautiful day for the fox to jump the fence!"
output = "" #The output buffer
temp_buf = "" #Temp storage for latin add-ons
word_found = False #Flag to identify a word has been found
cp = 0 # Character pointer to letter of interest in message
while cp < len(message):
ch = message[cp]
if whitespace.find(ch) >= 0 or punctuation.find(ch) >= 0:
word_found = False
if temp_buf:
output += temp_buf
temp_buf = ""
output += ch
else:
if word_found:
output += ch
else:
word_found = True
if vowels.find(ch.lower()) >= 0:
temp_buf = "ay"
output += ch
else:
temp_buf += ch + "ay"
cp += 1
if temp_buf:
output += temp_buf
print(output)
我将使用re.sub
的回调形式实现这一点,您可以在其中使用一个函数来确定正则表达式匹配的替换。
import re
vowels = "aeiou"
def mangle_word(match):
word = match.group(0)
if word[0].lower() in vowels:
return word + "ay"
return word[1:] + word[0] + "ay"
message = "Oh! my what a beautiful day for the fox to jump 653 fences!"
print(re.sub("[a-z]+", mangle_word, message, flags=re.I))
产出
Ohay! ymay hatway aay eautifulbay ayday orfay hetay oxfay otay umpjay 653 encesfay!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.