通过单词后面的第二个元音从字符串中获取子字符串

2024-05-14 20:28:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想这样做:

a = "Describe"

我想用后面的第二个元音单词来分割"Describe",所以它将是"Descri""be"。你知道吗

另一个例子是"Public",它应该分为"Pu""blic"。你知道吗

我试过:

vowel = "AaIiUuEeOo"
consonant = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz"
p = []
for char in words[::-1]:
    if char in consonant:
        p.append(char)
    elif char in vokal:
        p.append(char)
        break

我怎样才能做到这一点?你知道吗


Tags: inbepublic单词例子pu元音append
3条回答

可以使用字符串切片:

def s_split(s):
   l = [i for i in range(len(s)) if s[i].lower() in 'aeiou']
   return [s[:l[len(l) > 2]+1], s[l[len(l) > 2]+1:]]

输出:

['Pu', 'blic']
['Descri', 'be']
['i', 'e']
['i', 'bbe']
['Descri', 'be']

您可以使用regex ^(.*[AaIiUuEeOo])(?=.*[AaIiUuEeOo])(.+)$。我们的想法是捕捉到一个元音的所有内容,然后是另一个元音,还可以选择一些其他字符,这些字符将被放置在第二个捕捉组中。你知道吗

>>> import re
>>> pattern = r"^(.*[AaIiUuEeOo])(?=.*[AaIiUuEeOo])(.+)$"
>>> re.match(pattern, "Describe").groups()
('Descri', 'be')
>>> re.match(pattern, "Public").groups()
('Pu', 'blic')
>>> re.match(pattern, "ibe").groups()
('i', 'be')
>>> re.match(pattern, "ie").groups()
('i', 'e')

如果字符串至少没有两个元音,请确保在调用结果groups()之前测试None。你知道吗

另一个想法是使用^{},它比较笨重,但很有趣,可以很好地推广到任何n或任何字符集(或iterables,就这一点而言,我坚持使用字符串)。你知道吗

from itertools import groupby

def nth_from_rear(s, n=2, matches="aeiou"):
    def nth_counter(n=2, count=0):
        def cb(x):
            nonlocal count

            if x.lower() in matches:
                count += 1

            return count >= n

        return cb

    groups = groupby(s[::-1], key=nth_counter(n))
    return ["".join(x)[::-1] for _, x in groups][::-1]


if __name__ == "__main__":
    tests = [
        ["Public", 2],
        ["Describe", 2],
        ["ie", 2],
        ["ibbe", 2],
        ["Describe", 0],
        ["Describing", 1],
        ["Describe", 3],
        ["ababbaba", 4],
    ]

    for s, n in tests:
        print(s.rjust(10), n, nth_from_rear(s, n))

输出:

    Public 2 ['Pu', 'blic']
  Describe 2 ['Descri', 'be']
        ie 2 ['i', 'e']
      ibbe 2 ['i', 'bbe']
  Describe 0 ['Describe']
Describing 1 ['Describi', 'ng']
  Describe 3 ['De', 'scribe']
  ababbaba 4 ['a', 'babbaba']

如果希望不使用任何库:

vowel = "AaIiUuEeOo"
consonant = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz"
p = []
words = 'Public'
vowelCount = 0
secondPart = ''
firstPart = ''
for index, char in enumerate(words[::-1]):
    if char in vowel:
      vowelCount += 1
      if vowelCount == 2:
        firstPart = words[:-index]
        break
    secondPart += char
secondPart = secondPart[::-1]
print(firstPart, secondPart)

输出:

Pu blic

相关问题 更多 >

    热门问题