Python 在for循环内部使用字符串连接时出现异常行为

Python 在for循环内部使用字符串连接时出现异常行为,python,Python,因此,下面的代码按照预期正确地删除了字符串中的所有元音 def disemvowel(string): # Letters to remove & the new, vowerl free string vowels_L = list('aeiouAEIOU') new_string = "" # Go through each word in the string for word in string: # Go throug

因此,下面的代码按照预期正确地删除了字符串中的所有元音

def disemvowel(string):
    # Letters to remove & the new, vowerl free string
    vowels_L = list('aeiouAEIOU')
    new_string = ""

    # Go through each word in the string
    for word in string:

        # Go through each character in the word
        for character in word:

            # Skip over vowels, include everything elses
            if character in vowels_L:
                pass
            else:
                new_string += character

    # Put a space after every word
    new_string += ' '

    # Exclude space place at end of string
    return new_string[:-1]

no_vowels = disemvowel('Nasty Comment: Stack exchange sucks!')
print(no_vowels)

>>>>python remove_vowels.py
>>>>Nsty Cmmnt: Stck xchng scks!
然而,当我将语句“new_string+=””移动到我认为应该的位置时(我来自C/C++背景),我最终得到了一个奇怪的答案

def disemvowel(string):
    # Letters to remove & the new, vowerl free string
    vowels_L = list('aeiouAEIOU')
    new_string = ""

    # Go through each word in the string
    for word in string:

        # Go through each character in the word
        for character in word:

            # Skip over vowels, include everything elses
            if character in vowels_L:
                pass
            else:
                new_string += character

    # THIS IS THE LINE OF CODE THAT WAS MOVED        
    # Put a space after every word
    new_string += ' '

    # Exclude space place at end of string
    return new_string[:-1]

no_vowels = disemvowel('Nasty Comment: Stack exchange sucks!')
print(no_vowels)

>>>>python remove_vowels.py
>>>>N  s t y   C  m m  n t :   S t  c k    x c h  n g    s  c k s !

与其在一个单词被完全迭代后放置空格,还不如在有元音的地方放置空格。我希望有人能解释为什么会发生这种情况,即使在C中结果会完全不同。此外,任何关于精简/精简预算的建议都将受到欢迎!:)

对于字符串中的单词
不迭代单词;它对字符进行迭代。您根本不需要添加空格,因为原始字符串中的空格是保留的。

作为
interjay
注释,缩进已经过时了。Python依赖缩进来描述哪些语句属于哪个块,而不是更常见的
BEGIN
<代码>结束或
{
..
}

此外,您需要字符串中的单词,而字符串只是一个字符列表,而
for word In string
word
一次设置为
string
中的一个字符

使用
not in
比使用
if
pass
更干净

这更接近你的意图

def disemvowel(string):

    # Letters to remove & the new, vowel-free string
    vowels_list = 'aeiouAEIOU'
    new_string = ""

    # Go through each character in the string
    for character in string:

        # Skip over vowels, include everything else
        if character not in vowels_list:
            new_string += character

    return new_string


print disemvowel('Nasty Comment: Stack exchange sucks!')
输出

Nsty Cmmnt: Stck xchng scks!

您的代码在这里没有正确缩进,这使得不猜测代码实际缩进的方式就无法进行分析。对不起,我的疏忽。我修正了缩进。@martineau:请不要修正原始问题中的问题。它对解释如何纠正错误的解决方案毫无意义code@Borodin当前位置我所做的只是编辑问题标题。哦,这完全有道理!谢谢你的解释。OP的代码有更多的错误@Borodin:有,但是看起来像是做事情的笨拙方式,或者是当代码被复制到问题中时引入的格式错误。缩进问题看起来与您点击“代码”按钮,然后将代码粘贴到消息要求您放置的位置时所遇到的问题完全相同。@user2357112:可能吧,但我无法重现您所描述的内容。您是指代码段按钮Ctrl/M吗?这是针对JavaScript的,使一些东西完全不同。还是“代码示例”按钮Ctrl/K?这会在第一行添加额外的四个空格缩进,但在其他方面似乎没有问题。你能解释一下吗?@Borodin:带牙套的那个。如果点击该按钮,然后粘贴代码,则只有第一行获得它所需的额外4个空格,之后的所有内容都没有足够的缩进。这是一个长期存在的问题,一次又一次地困扰着新用户,对于缩进至关重要的Python问题来说尤其令人恼火。是的,我不知道为什么键入“word”会神奇地让Python为我正确解析字符串。谢谢你关于“不在”的提示,并改进了我发布的代码;指出好代码和坏代码之间的区别肯定能帮助我更快地学习这门语言,所以谢谢!