Python 如何对数组中的字符串进行操作?

Python 如何对数组中的字符串进行操作?,python,Python,我试图在一个句子上使用一个先前定义的函数,我已经把它分成了字符串,但我不知道如何循环它。请帮忙。代码如下 def pig_latin_sentence(sentence): ''' Converts a string of words separated by spaces to Pig Latin''' words=sentence.split(" ") for pig_latin(word) sentence2=sentence2+wo

我试图在一个句子上使用一个先前定义的函数,我已经把它分成了字符串,但我不知道如何循环它。请帮忙。代码如下

def pig_latin_sentence(sentence):
    ''' Converts a string of words separated by spaces to Pig Latin'''
    words=sentence.split(" ")
    for
        pig_latin(word)
        sentence2=sentence2+word
return sentence2

如果pig_latin函数适当地添加了空格,则循环应该如下所示:

def pig_latin_sentence(sentence):
    ''' Converts a string of words separated by spaces to Pig Latin'''
    words=sentence.split(" ")
    for word in words:
        pig_latin(word)
        sentence2=sentence2+word
return sentence2
但如果不是这样,那么使用join并使用map函数避免for循环可能会更好:

def pig_latin_sentence(sentence):
    ''' Converts a string of words separated by spaces to Pig Latin'''
    return ' '.join( map( pig_latin, sentence.split() ) )

对于word-in-words
?看起来您并没有遇到特定的问题,而是在编写代码时耗尽了精力。继续,你试过什么?预期的结果是什么?您似乎知道您需要一个for循环,那么如何在任何列表上循环?或者,如果没有循环:
sentence2=[pig\u拉丁语(word)表示句子中的单词。split(“”)