以下字符串,如果单词以辅音字母开头,则把辅音字母挪到最后,并在末尾加上“ay”。 如果以元音字母开头,则在末尾加上“hay”。 元音字母是“a.e.i.o.u” 字符串是“My name is Shopline,and i am 2,0000 days old”【杭州多测师】【杭州多测师_王sir】

author:多测师_王sir

def func(str):
    '''
    :param str: 传入一个字符串"My name is Shopline,and i am 2,0000 days old"
    :return:
    '''
    result = []
    s = str.split()   #先进行分割为列表
    for word in s:
        if word[0] in "aeiou":#第一个字母在‘aeiou'中末尾加入‘hay'
            temp1= word + "hay"
            result.append(temp1)  #['ishay', 'ihay', 'amhay', 'oldhay']
        elif word[0] not in "aeiou": #第一个字母不是‘aeiou’,连续的辅音字母一起移动到单词末尾加入“ay”
            temp =""
            index = 0
            temp += word[0]
            for i in range(1,len(word)):
                if word[i] not in "aeiouy":
                    temp += word[i]   #把首个字母不在"aeiou"里面字符串进行拼接
                    index += 1
                else:
                    break
            #如果判断每个列表中的元素首个字母不在"aeiou"里面、index索引位置需要加上1、然后把temp挪到后面再加上ay字符串
            result.append(word[index+1:] + temp + "ay")
    print(result)

func("My name is Shopline,and i am 2,0000 days old")
原文地址:https://www.cnblogs.com/xiaoshubass/p/15353623.html