Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在每个元音之前添加字符串_Python_Python 2.7 - Fatal编程技术网

Python在每个元音之前添加字符串

Python在每个元音之前添加字符串,python,python-2.7,Python,Python 2.7,我正在编写一个python代码,它采用一行输入的文本,并在每个元音之前添加字符串“tak”(y仅在不以单词开头的情况下作为元音计算),例如“我喜欢睡眠”将出现在“takI ltakitake sltakep”中。我刚刚开始学习编码,因此还不知道太多复杂的函数。下面是我到目前为止的代码,它根本不起作用 text = raw_input("enter the text to be translated") textSplit = text.split() count = 0 print len(te

我正在编写一个python代码,它采用一行输入的文本,并在每个元音之前添加字符串“tak”(y仅在不以单词开头的情况下作为元音计算),例如“我喜欢睡眠”将出现在“takI ltakitake sltakep”中。我刚刚开始学习编码,因此还不知道太多复杂的函数。下面是我到目前为止的代码,它根本不起作用

text = raw_input("enter the text to be translated")
textSplit = text.split()
count = 0
print len(textSplit)
Vowels = ["a","e","i","o","u"]
while count <= len(text):
  for i in textSplit:
    for i in Vowels:
      count += 1
text=raw\u输入(“输入要翻译的文本”)
textSplit=text.split()
计数=0
打印透镜(文本分割)
元音=[“a”、“e”、“i”、“o”、“u”]

当count时,以下方法可行:

text = 'I like sleep'
vowels = set("aeiou")
modified_text = ''.join(['tak'+letter if letter.lower() in vowels else letter for letter in text])
print(modified_text)
输出

takI ltakiktake sltaketakep

(注意:我认为您的示例输出中缺少了一个
k

如果您需要忽略任何前导的“y”,您必须稍微调整一下:

text = raw_input("enter the text to be translated")

temp1 = temp2 = text[1:] if text[0] in ['y', 'Y'] else text

for vowel in ['a', 'e', 'i', 'o', 'u', 'y']:
    temp2 = temp2.replace(vowel, 'tak{}'.format(vowel))
    temp2 = temp2.replace(vowel.upper(), 'tak{}'.format(vowel.upper()))

text = text.replace(temp1, temp2)
print text
对于“我喜欢睡眠”的输入,这将给出:

takI ltakiktake sltaketakep
“黄色”:

ytakelltakow
“土耳其”:

ttakurktaketaky
如果出于某种原因,您确实不想使用
str.replace
,您可以这样做:

text = raw_input("enter the text to be translated")

temp2 = text[1:] if text[0] in ['y', 'Y'] else text
vowels = ['a', 'e', 'i', 'o', 'u', 'y']

temp2 = ''.join(['tak{}'.format(letter) if letter.lower() in vowels else letter for letter in temp2])

if text[0] in ['y', 'Y']:
    text = text[0] + temp2
else:
    text = temp2

print text
您可以使用:

您还可以通过为每个元音在字符串上循环一次来使用。不要忘记字符串在Python中是不可变的,因此每次都必须创建一个新字符串:

>>> s="I like sleep"
>>> for v in 'aeiouAEIOU':
...    s=s.replace(v, 'tak'+v)
... 
>>> s
'takI ltakiktake sltaketakep'
在这种情况下,如果没有找到元音,则字符串
s
与以前相同,或者每个元音都被连接到元音的字符串
tak
替换。无论哪种情况,每次通过循环都会创建一个新字符串,并将其分配给
s

,您可以使用:

例如:

text = 'Text'
print(re.sub(r'([aeiou])', r'(exampleReplace)\1', text))
>> T(exampleReplace)ext
使用list()而不是split()。我还经常同时得到索引和值。 修改代码:

text = "I like banana"
textSplit = list(text)
Vowels = ["a","e","i","o","u"]
for index,letter in enumerate(textSplit):
    if letter.lower() in Vowels:
        textSplit[index] = "tak" + letter
print "".join(textSplit)
输出:

takI ltakiktake btakantakantaka

可能对你有用。我也在尝试同样的事情,但由于某种原因,元音在结果中消失了,这\1在替换文本中到底做了什么?它是捕获组
([aeiou])
,它是元音的发现我正在尝试打印
(re.sub(r'([aeiou]),r'(exampleplace'),text))
,这将替换元音,如果我使用
r'(exampleplace)\1'
作为替换字符串,元音将保留在那里。我提供了一个解决方案,可以避免
str.replace
text = "I like banana"
textSplit = list(text)
Vowels = ["a","e","i","o","u"]
for index,letter in enumerate(textSplit):
    if letter.lower() in Vowels:
        textSplit[index] = "tak" + letter
print "".join(textSplit)
takI ltakiktake btakantakantaka