Python 如何将字符串仅附加到列表中的辅音?

Python 如何将字符串仅附加到列表中的辅音?,python,list,python-2.7,Python,List,Python 2.7,现在我正试图创造一个令人反感的译者。也就是说,在一行中的一个辅音或几个辅音之后,将“op”添加到这些字母中。例如,cow将成为copowop或street,后者将成为Strokeetop。这就是我到目前为止所做的: def oppish(phrase): #with this function I'm going to append 'op' or 'Op' to a string. consonants = ['b','c','d','f','g','h','i','j','k','

现在我正试图创造一个令人反感的译者。也就是说,在一行中的一个辅音或几个辅音之后,将“op”添加到这些字母中。例如,cow将成为copowop或street,后者将成为Strokeetop。这就是我到目前为止所做的:

def oppish(phrase): #with this function I'm going to append 'op' or 'Op' to a string.
    consonants =  ['b','c','d','f','g','h','i','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
    vowels = ['a', 'e', 'i', 'o', 'u'] #this and the preceding line create a variable for the vowels we will be looking to append 'op' to.    
    if phrase == '':    #error case. if the input is nothing then the program will return False.
        return False
    phrase_List = list(' ' + phrase) # turns the input phrase into a list which allows for an        index to search for consonants later on.
    new_phrase_List = list() #creates new list for oppish translation
    for i in range(1, len(phrase_List)):
        if phrase_List[i] == phrase_List[1]:
            new_phrase_List.append(phrase_List[i])
        elif phrase_List[i] in consonants:
            new_phrase_List.append('op') #adds op to the end of a consonant within the list and then appends it to the newlist
        new_phrase_List.append(phrase_List[i]) #if the indexed letter is not a consonant it is appended to the new_phrase_list.
    print 'Translation: ' + ''.join(new_phrase_List)

oppish('street')
这里唯一的问题是上面的代码会产生这样的结果

Translation: ssoptopreeopt

我不确定我做错了什么,我试过使用可视化工具,但没有用。感谢您的帮助!:)

我认为问题在于你处理问题的方法。 尝试这样做:

编辑:虽然这个问题有一个更好的(pythonic)答案(多亏了dano),但这个答案并不需要额外的库

vowels = ['a', 'e', 'i', 'o', 'u']

def oppish(word):
    result = []
    first = True
    prev_vowel = False

    for letter in list(word):
        if (letter in vowels) and (not first) and (not prev_vowel):
            result.append('op')
            prev_vowel = True
        else:
            prev_vowel = False

        result.append(letter)
        first = False
        if not prev_vowel:
            result.append('op')
        print ''.join(result)


oppish('street')

#> stropeetop
提示:不要浪费时间定义元音和辅音。事实上,有元音和非元音,这非常适合,这将允许您使用键函数在iterable中对项目进行分组。组将累积,直到键函数的返回值发生变化,此时
group
by将生成键函数的返回值和累积组上的迭代器。在本例中,如果字母是元音,我们希望键函数返回
True
。这样,我们将从
groupby
中获得连续辅音组和连续元音组:

from itertools import groupby

vowels = {'a', 'e', 'i', 'o', 'u'}  # set instead of list, because lookups are O(1)

def oppish(phrase):
    if not phrase:
        return False

    out  = []
    for is_vowel, letters in groupby(phrase, lambda x: x in vowels):
        out.append(''.join(list(letters)))
        if not is_vowel:
            out.append('op')
    return ''.join(out)


print oppish('street')
print oppish('cow')
输出:

stropeetop
copowop

嗯,你的辅音列表中有
i
,这是什么意思?我是否应该使用“I”索引检查辅音,然后将它们附加到我创建的新列表中?@JohnathanScott:
'I'
是字符串
I
。它与
i
变量无关。你希望短语列表[i]==短语列表[1]做什么?这并不影响这个特定的用例,但我是说你的辅音列表和元音列表中都有字母
'i'
。不要与索引变量
i
混淆。另外,我认为您的翻译是反向的。你应该做
辅音=>辅音
元音=>'op'+元音