现在我正在尝试创建一个oppish翻译器。也就是说,在一个辅音或几个辅音连续之后,你给这些字母加上'op'。作为一个例子,牛会成为copowop或街道,这将成为stropeetop。这是我到目前为止有:如何将字符串追加到列表中的辅音中?
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
我不知道我做错了,我试着去通过一个可视化,但无济于事。所有帮助表示赞赏! :)
那么一个你'i'在辅音列表 – bwegs 2014-09-27 15:33:50
如何你的意思是?我不应该使用'我'来检查辅音索引,然后将它们追加到我创建的新列表中? – 2014-09-27 15:35:31
@JohnathanScott:''我''是字符串'我'。它与你的'i'变量没有关系。你期望'phrase_List [i] == phrase_List [1]'做什么? – Blender 2014-09-27 15:37:09