在 Python 中将句子转换为 Piglatin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23177250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:24:59  来源:igfitidea点击:

Converting a sentence to piglatin in Python

pythonpython-3.x

提问by user3534918

I have to write a program in Python that will convert sentence into pig Latin. Pig Latin is loosely defined as taking the first letter of each word, putting it at the end of the word, and adding "ay" to the end of each word. I can't figure out how to separate the first letter from each word in the string much less add it to the end.I assume once I get it removed there's a way to concatenate it to the new word then concatenate "ay" as well. I'm extremely lost here. After tons of trial and error This is all I have, and even this doesn't seem to be working right. Any help is much appreciated.

我必须用 Python 编写一个程序,将句子转换为pig Latin。Pig Latin 的松散定义是取每个单词的第一个字母,将其放在单词的末尾,并在每个单词的末尾添加“ay”。我不知道如何将字符串中的每个单词的第一个字母分开,更不用说将其添加到末尾。我假设一旦将其删除,就有一种方法可以将它连接到新单词然后连接“ay” . 我在这里非常迷茫。经过大量的反复试验这就是我所拥有的,甚至这似乎也不能正常工作。任何帮助深表感谢。

def main():        
    sentence = input('Type what you would like translated into pig-latin and press ENTER: ')
    sentence_list = sentence.split()

    for part in sentence_list:
        first_letter = part[0]

main()

回答by Evan

Here is a quick one

这是一个快速的

def main():
    words = str(input("Input Sentence:")).split()
    for word in words:
        print(word[1:] + word[0] + "ay", end = " ")
    print ()

main()

A better solution would probably use list comprehension so you could actually use the output, but this does what you asked.

更好的解决方案可能会使用列表理解,因此您可以实际使用输出,但这会满足您的要求。

EDIT: This works for python3.x If you want it to work for python2 you are going to have a bit more fun. Just add the strings together for each word, then print the result string.

编辑:这适用于 python3.x 如果您希望它适用于 python2,您将获得更多乐趣。只需将每个单词的字符串加在一起,然后打印结果字符串。

回答by A.J. Uppal

Here is the code:

这是代码:

def main():
        lst = ['sh', 'gl', 'ch', 'ph', 'tr', 'br', 'fr', 'bl', 'gr', 'st', 'sl', 'cl', 'pl', 'fl']
        sentence = input('Type what you would like translated into pig-latin and press ENTER: ')
        sentence = sentence.split()
        for k in range(len(sentence)):
                i = sentence[k]
                if i[0] in ['a', 'e', 'i', 'o', 'u']:
                        sentence[k] = i+'ay'
                elif t(i) in lst:
                        sentence[k] = i[2:]+i[:2]+'ay'
                elif i.isalpha() == False:
                        sentence[k] = i
                else:
                        sentence[k] = i[1:]+i[0]+'ay'
        return ' '.join(sentence)

def t(str):
        return str[0]+str[1]

if __name__ == "__main__":
        x = main()
        print(x)

Runs as:

运行如下:

bash-3.2$ python3 pig.py
Type what you would like translated into pig-latin and press ENTER: my gloves are warm
ymay ovesglay areay armway
bash-3.2$ 

This code uses the logic found here.

此代码使用此处找到的逻辑。