【问题标题】:split string at vowel + surrounding consonants在元音+周围辅音处拆分字符串
【发布时间】:2020-07-15 22:06:57
【问题描述】:

我是编码新手,这是我的第一次尝试。我想将单词从语音语言中分成音节。

从语音语言中用单词组成音节的规则:

考虑到第一个元音之前的所有辅音,考虑那个元音。 重复。

例子:

ma - ri- a

a - le - ksa - nda - r

这就是我已经走了多远:

    word = 'aleksandar'
    vowels = ['a','e','i','o','u']
    consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

    for vowel in vowels:

        if vowels in word:

            index_1 = int(word.index(vowel)) - 1
            index_2 = int(word.index(vowel)) + 1

            print(word[index_1:index_2])

        else:

            print(consonants)

IDK出了什么问题,请帮忙! 在此先感谢:)

【问题讨论】:

    标签: python string split slice


    【解决方案1】:

    这应该可以解决您的问题

    word = 'aleksandar'
    vowels = ['a','e','i','o','u']
    
    new_word = ""
    
    for letter in word:
        if letter in vowels:
            new_word += letter + "-"
        else:
            new_word += letter
    
    print(new_word)
    

    【讨论】:

      【解决方案2】:

      我稍微修改了你的代码,它运行良好!!!

      word = 'aleksandar'
      word = list(word)
      vowels = ['a','e','i','o','u']
      s = ""
      syllables = [ ]
      for i in range(len(word)):
          if word[i] not in vowels:
              s = s + word[i]
          else:
              s = s + word[i]
              syllables.append(s)
              s = ""
      print(syllables)    
      

      输出是:

      ['a', 'le', 'ksa', 'nda']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 1970-01-01
        • 2018-11-16
        • 2019-12-09
        • 1970-01-01
        • 2023-03-22
        • 2017-08-02
        相关资源
        最近更新 更多