gpt4 book ai didi

python - 如何捕获字符串中直到第一个元音的字符并从中创建子字符串?

转载 作者:行者123 更新时间:2023-11-28 22:39:36 25 4
gpt4 key购买 nike

目前,我的代码的第一部分工作正常。如果它检测到第一个字符(索引 0)是元音,它就会停止并在单词末尾添加“yay”。

第二部分旨在捕获第一个元音之前的辅音。这工作正常。

当我尝试使用原始单词并将所有内容切掉直到第一个元音并从中创建一个新的子字符串时,问题就出现了。这意味着如果用户输入“hello”,它应该输出“ellohay”。我可以听到“hellohay”,但不知道如何捕捉那些初始辅音并将它们切掉。

# Pig Latinify

vowels = ['a', 'e', 'i', 'o', 'u']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z']


def pig_latinify():
state = True
while state == True:

user_input = raw_input("Enter a word to be translated: ").lower()

# If the first character in input is a vowel add 'yay' to input and print.
if user_input[0] in vowels[0:]:
print ""
print "Begins with a vowel."
pig_output = user_input + "yay"
print user_input, "becomes:", pig_output
print ""
else:
print ""
print "Doesn't begin with a vowel."

captured_consonants = ""
captured_substring = ""
new_user_input = ""

# Capture the consonants up to the first vowel
for i in user_input:
if i in vowels:
break
if i in consonants:
captured_consonants = captured_consonants + i

# Slice user_input up to the first vowel and create a substring beginng from the first vowel until the end of the string.
if i in consonants:
break
if i in vowels:
captured_substring = captured_substring + i
print captured_substring

# Concatenate substring of user_input with captured_consonants and 'ay'
pig_output = captured_substring + captured_consonants + "ay"
print user_input, "becomes:", pig_output
print ""

pig_latinify()

最佳答案

使用正则表达式可能是最好的选择:

# Pig Latinify
import re

vowels = list('aeiou')

def pig_latinify():
state = True
while state == True:

user_input = raw_input("Enter a word to be translated: ").lower()

# If the first character in input is a vowel add 'yay' to input and print.
if user_input[0] in vowels[0:]:
print ""
print "Begins with a vowel."
pig_output = user_input + "yay"
print user_input, "becomes:", pig_output
print ""
else:
print ""
print "Doesn't begin with a vowel."

r = re.search("(.*?)([aeiou].*)", user_input)

# Capture the consonants up to the first vowel
captured_consonants = r.groups()[0]

# Slice user_input up to the first vowel and create a substring beginng from the first vowel until the end of the string.
captured_substring = r.groups()[1]

# Concatenate substring of user_input with captured_consonants and 'ay'
pig_output = captured_substring + captured_consonants + "ay"
print user_input, "becomes:", pig_output
print ""

pig_latinify()

这基本上是非贪婪地搜索字符串,直到它遇到元音,然后将辅音和元音+后续字符串分成两组,并相应地对其进行操作。

$ python pig_latin.py 
Enter a word to be translated: hello

Doesn't begin with a vowel.
hello becomes: ellohay

Enter a word to be translated: hi

Doesn't begin with a vowel.
hi becomes: ihay

Enter a word to be translated: apple

Begins with a vowel.
apple becomes: appleyay

关于python - 如何捕获字符串中直到第一个元音的字符并从中创建子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34550817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com