[英]Python add string before every vowel
I am writing a python code that takes a single imputed line of text and adds the string "tak" before every vowel (y only counting as a vowel if it doesn't begin a word) for example "I like sleep" would come out to "takI ltakitake sltaketakep". 我正在编写一个python代码,该代码接受一行插入的文本,并在每个元音之前添加字符串“ tak”(如果不开始单词,则仅算作元音),例如“我喜欢睡觉”到“ takI ltakitake sltaketakep”。 I am only just beginning learning to code and as such do not know too many complex functions yet.
我只是刚刚开始学习编码,因此还不知道太多复杂的功能。 Below is the code I have thus far, which isn't working at all.
下面是到目前为止我没有的代码。
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
However, I have not been able to figure out how I can slice the string, add "tak" and concatenate it. 但是,我还无法弄清楚如何分割字符串,添加“ tak”并将其连接起来。 Thank you.
谢谢。
edit: can it be done without using the text.replace
module? 编辑:可以不使用
text.replace
模块来完成吗?
You can use a regex : 您可以使用正则表达式 :
>>> re.sub(r'(?i)([aeiou])', r'tak\1', "I like sleep")
'takI ltakiktake sltaketakep'
You can also use str.replace by looping over the string once for each vowel. 您还可以通过为每个元音循环一次字符串来使用str.replace 。 Don't forget that strings are immutable in Python so you have to create a new string each time:
不要忘记字符串在Python中是不可变的,因此您每次都必须创建一个新字符串:
>>> s="I like sleep"
>>> for v in 'aeiouAEIOU':
... s=s.replace(v, 'tak'+v)
...
>>> s
'takI ltakiktake sltaketakep'
In this case, the string s
is either the same as before if the vowel is not found or each vowel is replaced by the string tak
concatenated to the vowel. 在这种情况下,如果未找到元音,则字符串
s
与以前相同,或者每个元音都由连接到元音的字符串tak
替换。 A new string is created in either case each time through the loop and assigned to s
. 无论哪种情况,每次通过循环都会创建一个新字符串,并将其分配给
s
。
If you need to ignore any leading 'y's, you'll have to finesse it just a bit: 如果您需要忽略任何前导“ 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
For an input of 'I like sleep', this gives: 对于“我喜欢睡觉”的输入,它给出:
takI ltakiktake sltaketakep
'yellow': '黄色':
ytakelltakow
'turkey': '火鸡':
ttakurktaketaky
If, for some reason, you really didn't want to use str.replace
, you could do it like this: 如果由于某种原因,您真的不想使用
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
The following would work: 以下将起作用:
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)
Output 产量
takI ltakiktake sltaketakep
(note: I think you are missing a k
in your sample output) (注意:我认为您在示例输出中缺少了
k
)
Use list() instead of split() . 使用list()而不是split() 。 I have also used enumerate to get the index and value simultaneously.
我还使用枚举来同时获取索引和值。 Modified code:
修改后的代码:
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)
Output: 输出:
takI ltakiktake btakantakantaka
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.