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

How can I capture characters in a string up to the first vowel and create substring out of it?

目前,我的代码的第一部分工作正常。如果它检测到第一个字符(索引 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()

如果你想删除所有前导字符,直到你遇到元音,你可以使用 itertools.dropwhile:

from itertools import dropwhile
user_input = "hello"

vowels = {"a","e","i","o","u"}

up_to = "".join(dropwhile(lambda x: x not in vowels, user_input))
print(up_to + user_input[:len(user_input) - len(up_to)]+"ay")

输出:ellohay

lambda x: x not in vowels 表示我们要删除所有字符,直到找到元音字符。如果你想为大写或小写工作,请将大写元音添加到集合中或将 lambda 切换为 x.lower() not in vowels

这段代码看起来很奇怪。例如,第二个 if i in vowels: 永远不会达到。

除此之外,您可能还想:

  1. 找到第一个元音的位置-让它成为pos
  2. 检查是否 pos > 0
  3. return user_input[pos:] + user_input[:pos-1] + 'ay'

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

# 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()

这基本上是 non-greedily 搜索字符串直到它遇到元音,然后将辅音和元音+后续字符串分成两组,并相应地操作它们。

$ 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