【问题标题】:How Do I Fix This String Issue in Python?如何在 Python 中解决此字符串问题?
【发布时间】:2012-09-15 09:40:58
【问题描述】:

我正在尝试在某些单词中添加带有元音的文本(不是像 ie 或 ei 这样的连续元音),例如:

单词:“奇怪”

要在元音前添加的文本:'ib'

结果:'wibeird'

因此,在元音“e”之前添加了文本“ib”。请注意它没有将 'i' 替换为 'ib',因为当元音是连续的时,我不希望它添加文本。

但是,当我这样做时:

单词:“狗”

要在元音之前添加的文本:'ob'

结果:'doboog'

正确的结果应该是:'dobog'

我一直在尝试调试我的程序,但我似乎无法弄清楚逻辑以确保它正确打印“wibeird”和“dobog”。

这是我的代码,先将 first_syl 替换为 'ob',然后将 word 替换为 'dog',然后再将其替换为 'weird'。

first_syl = 'ib'
word = 'weird'

vowels = "aeiouAEIOU"
diction = "bcdfghjklmnpqrstvwxyz"
empty_str = ""
word_str = ""
ch_str = ""
first_vowel_count = True

for ch in word:
    if ch in diction:
        word_str += ch
    if ch in vowels and first_vowel_count == True:
        empty_str += word_str + first_syl + ch
        word_str = ""
        first_vowel_count = False
    if ch in vowels and first_vowel_count == False:
        ch_str = ch
    if word[-1] not in vowels:
        final_str = empty_str + ch_str + word_str 

print (final_str)

我正在使用 Python 3.2.3。另外我不想使用任何导入的模块,试图这样做以了解python中字符串和循环的基础知识。

【问题讨论】:

  • 请注意,您不是在替换元音,而是在元音前面插入一个字符串。如果我们替换它,在将 'o' 替换为 'ob' 之后,'dog' 将变为 'dobg'。
  • 在您的第一个示例中,看起来您根本没有替换“e”-“wibeird”中仍然有一个 e
  • 对不起,我不是要“替换”我实际上是想在元音之前添加文本,我很抱歉。我现在在描述中解决了这个问题。

标签: python string loops


【解决方案1】:

你考虑过正则表达式吗?

import re

print (re.sub(r'(?<![aeiou])[aeiou]', r'ib\g<0>', 'weird')) #wibeird
print (re.sub(r'(?<![aeiou])[aeiou]', r'ob\g<0>', 'dog')) #dobog

【讨论】:

  • 对不起,我之前没有提到这一点,我宁愿不使用导入的模块和正则表达式,尝试这样做以了解python中字符串和循环的基础知识..谢谢。
【解决方案2】:

不要在不必要的时候使用正则表达式。有一句名言是这样的

有些人在遇到问题时会想 “我知道,我会使用正则表达式。”现在他们有两个问题。

这可以通过基本的 if-then 语句轻松解决。这是解释所使用逻辑的注释版本:

first_syl = 'ib' # the characters to be added
word = 'dOg'     # the input word

vowels = "aeiou" # instead of a long list of possibilities, we'll use the 
                 # <string>.lower() func. It returns the lowercase equivalent of a 
                 # string object.
first_vowel_count = True # This will tell us if the iterator is at the first vowel
final_str = ""           # The output.

for ch in word:
    if ch.lower() not in vowels:     # If we're at a consonant, 
        first_vowel_count = True     # the next vowel to appear must be the first in 
                                     # the series.

    elif first_vowel_count:          # So the previous "if" statement was false. We're 
                                     # at a vowel. This is also the first vowel in the 
                                     # series. This means that before appending the vowel 
                                     # to output, 

        final_str += first_syl       # we need to first append the vowel-
                                     # predecessor string, or 'ib' in this case.
        first_vowel_count = False    # Additionally, any vowels following this one cannot 
                                     # be the first in the series.

    final_str += ch                  # Finally, we'll append the input character to the 
                                     # output.
print(final_str)                     # "dibOg"

【讨论】:

  • 我喜欢这句话,哇,你一定是个天才,我显然把事情复杂化了,学习这对我来说很难,但很有趣。有没有办法在这个网站上给你发消息,我可能还有一些问题,因为我接下来打算做的是在第一个元音之后取每个元音,并在它之前添加不同的文本集(比如 second_syl、third_syl 等)?
  • @Ratman2050 没有办法发送私人信息;这个评论系统是私密的。如果您有更多问题,请随时为他们发更多帖子,我相信您会得到同样有用的反馈
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2020-02-16
  • 2019-10-26
  • 2017-05-09
  • 2021-09-20
相关资源
最近更新 更多