【问题标题】:Python Program to continuously move consonants to end of string until the first letter is vowelPython程序不断将辅音移动到字符串的末尾,直到第一个字母是元音
【发布时间】:2013-11-07 05:24:28
【问题描述】:

例如,如果我输入bob,它应该给我obb。同样,plank 之类的内容应该给我ankpl

s = input("What word do you want translated?")
first = s[0]
vowel = "aeiou"
for i in range (1, len(s)):
     if first in vowel:
        s = s + "way"
        print (s)
else:
        s = s[1:] + s[0]
        print (s)

这目前只给我lankp for plank。 谢谢!

【问题讨论】:

  • atwhay areway ouyay yingtray ootay ooday?

标签: python string loops


【解决方案1】:

实际上可以更简单:

s = raw_input("What word do you want translated?").strip()
vowel = set("aeiou")
if vowel & set(s):
    while s[0] not in vowel:
        s = s[1:] + s[0]
    print s
else:
    print "Input has no vowels"

【讨论】:

  • 我想多了,谢谢你的回答!问题:.strip() 有什么作用?
  • 将元音设为一组会很好,尽管对于这个问题,显然没有必要(即vowel = set("aeiou")
  • @Anonmly 首先,raw_input 是你想要的,因为只是简单的 input 对用户输入进行 eval。 strip 删除字符串开头和结尾的空格。
  • @JasonSundram 专为您编辑。
  • 如果里面没有元音怎么办?
【解决方案2】:

您只设置一次first = s[0],并且在循环之前完成。您可能希望将其设置在 for 循环中。

【讨论】:

    【解决方案3】:

    搜索元音然后将字符串旋转一次可能效率更高:

    vowels = 'aeiou'
    
    for index, character in enumerate(s):
        if character in vowels: break
    
    s = s[index:] + s[:index]
    

    【讨论】:

      【解决方案4】:

      您的程序的问题是 else 没有缩进到正确的级别,因此您有一个 for/else 构造而不是 if/else

      这是一种更有效的方法。

      如果vowels = set("aeiou"),可以这样得到第一个元音的位置

      next(i for i, j in enumerate(s) if j in vowels)
      

      例如:

      >>> s = "plank"
      >>> vowels = set("aeiou")
      >>> next(i for i, j in enumerate(s) if j in vowels)
      2
      

      >>> s[2:] + s[:2]
      'ankpl'
      

      所以现在你只需要操作字符串一次。

      如果没有元音,代码会引发异常而不是永远运行:)

      >>> s="why why why?"
      >>> next(i for i, j in enumerate(s) if j in vowels)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      StopIteration
      

      【讨论】:

        【解决方案5】:

        这会起作用

        #Move Consonants to the End
        def MoveConsonants(input1):
            contains_vowel = False
            for letter in input1: #Check if the work contains a vowel or not
                if letter in 'aeiou':
                    contains_vowel = True
            #If the word doesn't contain a vowel, return the same word
            if not contains_vowel: 
                return input1
        
            #Check if the first letter is a vowel, if so, we can return the string
            if input1[0] in 'aeiou':
                return input1
        
            #if the first letter is not a vowel, move the first letter to the end and repeat the process
            input1 = input1[1:] + input1[0]
            return MoveConsonants(input1)
        print(MoveConsonants('Plank'))
        

        【讨论】:

        • 如果您解释为什么这会起作用,这将对 OP 有所帮助。 “给男人一条鱼……”
        猜你喜欢
        • 2021-06-20
        • 2020-06-07
        • 2017-04-12
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        • 2017-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多