我需要定义一个函数weird_latin_from_sentence,该函数将获取一个句子字符串并返回该句子的拉丁版本。您可能还需要定义另一个名为weird_latin_word的函数,该函数接受一个单词并将其转换为怪异的拉丁语。转换规则为:
1. If the word starts with a consonant then the weird Latin version is formed by moving the first letter to the end of the word and adding the string "eeoow", eg, turtle -> urtleteeoow
2. If the word starts with a non-consonant character then the weird Latin version is simply the English word followed by "meeoow", eg, egg -> eggmeeoow and 121word -> 121wordmeeoow
对于此任务,以下是辅音:
bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ
其他所有内容,包括数字都是非辅音。 到目前为止,我已经能够用一个词做到这一点。这是我的程序:
def weird_latin_from_word(english):
"""Sub"""
weird_latin = ""
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
for i in range(len(english)):
if consonants[i] == english[0][i]:
english[i] = english[1:] + "eeoow"
weird_latin = weird_latin + english
return (english)
else:
english[i] = english[i] + "meeoow"
return (english)
如何为句子字符串写这个?使用“列表理解”代替地图功能
例如:
weird_latin = weird_latin_from_sentence("Toby likes his art")
输出:
obyteeoow ikesleeoow isheeoow artmeeoow
print(weird_latin)
答案 0 :(得分:0)
您可以改为使用in
运算符来执行辅音检查。然后将句子拆分成单词并映射到单词处理功能:
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
def weird_latin_from_word(english):
if english[0] in consonants:
return english[1:] + english[0] + 'eeoow'
return english + 'meeoow'
def weird_latin_from_sentence(sentence):
return ' '.join(weird_latin_from_word(w) for w in sentence.lower().split())
这样:
print(weird_latin_from_sentence("Toby likes his art"))
输出:
obyteeoow ikesleeoow isheeoow artmeeoow