为列表中的每个项目分配一个新变量 - Python

时间:2013-11-12 12:45:41

标签: python python-2.7

这是codeacademy中使用的标准猪拉丁代码。它运作良好,但它的缺点是它一次只适用于一个单词:

pyg = 'ay'

original = raw_input('Enter a word or phrase:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    translate = word[1:] + word[0]
    if word[0] != "a" and word[0] != "e" and word[0] != "i" and word[0] != "o" and word[0] != "u":
        new_word = translate + pyg
        print new_word
    else:
        new_word = word + pyg
        print new_word
else:
    print 'Input is empty or illegal'

所以我想这样做就可以了。这就是我想出的:

pyg = 'ay'

count = 0

original_input = raw_input('Enter a word or phrase:')

original = original_input

original_list = []

#converts to a list
while " " in original:
    if count > 50:
        break
    word = original[0:original.index(" ")]
    original_list.append(word)
    space = original.index(" ")
    space += 1
    original = original[space:]
    count += 1
#this works great until there is a word left and no spaces i.e. the last word
if len(original) > 0:
    original_list.append(original)
#this adds the last word

print original_list

def pyglatin(phrase):
#old code doesn't work because phrase is a list
        #now I have to translate BACK to a string
        for words in phrase:
            new_word = str(words)
    """this works for one word, how do I assign a new variable for every word if I don't know the phrase length ahead of time"""

所以这让我想到了一个问题:当我不知道我需要多少项时,如何为每个项目分配一个变量,然后能够回复该代码(通过旧的pyglatin译者)?

2 个答案:

答案 0 :(得分:0)

也许是这样的?

def latinize(word):
        word = word.lower()
        translate = word[1:] + word[0]
        return (translate if word[0] in 'aeiou' else word) + 'ay'

original = raw_input('Enter a word or phrase:')
print ' '.join(latinize(word) for word in original.split())

输入和输出示例:

Enter a word or phrase:Gallia est omnis divisa in partes tres
galliaay steay mnisoay divisaay niay partesay tresay

但实际上你不应该把所有的初始辅音都移到最后,而不仅仅是第一个吗?

编辑:

如果您想将所有初始辅音移到最后,您可以使用:

def latinize(word):
        word = word.lower()
        firstvowel = min (word.index (v) if v in word else 42042 for v in 'aeiou')
        if firstvowel == 42042: raise Exception ('No vowel in word')
        return word [firstvowel:] + word [:firstvowel] + 'ay'

original = raw_input('Enter a word or phrase:')
print ' '.join(latinize (word) for word in original.split () )

示例输入和输出:

Enter a word or phrase:star chick mess string
arstay ickchay essmay ingstray

答案 1 :(得分:0)

正如Maxime所提到的,列表在这个实例中很有用,并且字符串可以更容易地转换为列表。为了解释短语,我会按如下方式重写代码:

def trans_word(original):
    if len(original) > 0 and original.isalpha():
        word = original.lower()
        translate = word[1:] + word[0]
        if word[0] != "a" and word[0] != "e" and word[0] != "i" and word[0] != "o" and word[0] != "u":
            new_word = translate + pyg
            return new_word
        else:
            new_word = word + pyg
            return new_word
    else:
        return "Input is empty or illegal"


def pyglatin(phrase):
    if len(phrase) > 0:
        return " ".join(trans_word(word) for word in phrase)
    else:
        return "Input is empty or illegal"


pyg = 'ay'
count = 0
original_input = raw_input('Enter a word or phrase:')
original_list = original_input.split()

print pyglatin(original_list)