Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中修复此字符串问题?_Python_String_Loops - Fatal编程技术网

如何在Python中修复此字符串问题?

如何在Python中修复此字符串问题?,python,string,loops,Python,String,Loops,我试图在某些单词中添加带有元音的文本(不是像ie或ei那样的连续元音),例如: 单词:“怪异” 要在元音“ib”之前添加的文本 结果:“wibeird” 因此,在元音“e”之前添加了文本“ib”。请注意,它没有将“i”替换为“ib”,因为当元音连续时,我不希望它添加文本 但是,当我这样做时: 单词:“狗” 要在元音“ob”之前添加的文本 结果:“多博格” 正确的结果应该是:“dobog” 我一直在试着调试我的程序,但我似乎无法找出逻辑,以确保它正确打印“wibeird”和“dobog” 这是我的

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

单词:“怪异”

要在元音“ib”之前添加的文本

结果:“wibeird”

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

但是,当我这样做时:

单词:“狗”

要在元音“ob”之前添加的文本

结果:“多博格”

正确的结果应该是:“dobog”

我一直在试着调试我的程序,但我似乎无法找出逻辑,以确保它正确打印“wibeird”和“dobog”

这是我的代码,先用'ob'替换first_syl,用'dog'替换word,然后用'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中字符串和循环的基本知识。

您考虑过正则表达式吗

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
重新导入
印刷品(re.sub(r’(?’,‘怪异’)#wibeird
打印(re.sub(r'(?','dog'))#dobog

您考虑过正则表达式吗

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
重新导入
印刷品(re.sub(r’(?’,‘怪异’)#wibeird
打印(re.sub(r'(?','dog'))#dobog

不必使用正则表达式时,千万不要使用正则表达式。有句名言是这样说的

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

这可以用基本的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"
first_syl='ib'#要添加的字符
word='dOg'#输入字
元音=“aeiou”#我们将使用
#.lower()函数。它返回与
#字符串对象。
first_元音_count=True#这将告诉我们迭代器是否位于第一个元音处
最终输出。
对于ch in word:
如果ch.lower()不是元音:#如果我们是辅音,
first_元音_count=True#出现的下一个元音必须是中的第一个元音
#这个系列。
elif first_元音计数:#那么前面的“if”语句是错误的。我们
#第一个元音。这也是第一个元音
#这意味着在附加元音之前
#输出,,
final_str+=first_syl#我们需要先附加元音-
#前置字符串,在本例中为“ib”。
first_元音_count=False#此外,此元音后面的任何元音都不能
#成为系列中的第一个。
final_str+=ch#最后,我们将把输入字符附加到
#输出。
打印(最终打印)#“dibOg”

不必使用正则表达式时,千万不要使用正则表达式。有句名言是这样说的

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

这可以用基本的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"
first_syl='ib'#要添加的字符
word='dOg'#输入字
元音=“aeiou”#我们将使用
#.lower()函数。它返回与
#字符串对象。
first_元音_count=True#这将告诉我们迭代器是否位于第一个元音处
最终输出。
对于ch in word:
如果ch.lower()不是元音:#如果我们是辅音,
first_元音_count=True#出现的下一个元音必须是中的第一个元音
#这个系列。
elif first_元音计数:#那么前面的“if”语句是错误的。我们
#第一个元音。这也是第一个元音
#这意味着在附加元音之前
#输出,,
final_str+=first_syl#我们需要先附加元音-
#前置字符串,在本例中为“ib”。
first_元音_count=False#此外,此元音后面的任何元音都不能
#成为系列中的第一个。
final_str+=ch#最后,我们将把输入字符附加到
#输出。
打印(最终打印)#“dibOg”

请注意,您不是在替换元音,而是在元音前插入一个字符串。如果我们将其替换,则“dog”将在“o”替换为“ob”后变为“dobg”。在第一个示例中,看起来您根本没有替换“e”-“wibeird”中仍然有一个e抱歉,我不是要“替换”我实际上是想在元音之前添加文本,我很抱歉。我现在已在说明中解决了这一问题。请注意,您不是替换元音,而是在元音前面插入字符串。如果我们将其替换,则“dog”在将“o”替换为“ob”后将变成“dobg”。在您的第一个示例中,它看起来不像您是在重复在“e”这个词上得分很高-在“wibeird”中还有一个“e”抱歉,我不是要“替换”我实际上是想在元音之前添加文本,我很抱歉。我现在在描述中已经解决了这个问题。抱歉,我之前没有提到这一点,我不想使用导入的模块和正则表达式,试着这样做来理解python中字符串和循环的基本知识。谢谢。抱歉,我之前没有提到这一点,我不想不要使用导入的模块和正则表达式,尝试这样做是为了理解python中字符串和循环的基本知识。谢谢。我喜欢这句话,哇,你一定是个geni