簡體   English   中英

我在使用Python中的音節邊界算法時遇到麻煩

[英]I am having a trouble with an algorithm for assinging syllable boundaries in Python

請問你能幫幫我嗎?

我正在嘗試設計一種算法,該算法可對任何給定的輔音和元音“ CVCCVCCVCC”字符串的音節邊界進行評估。 這些字符串存儲在一個列表中,該列表的長度可能達到數十萬甚至上百萬個。 但是,似乎我不了解如何正確地處理列表,這就是我遇到此麻煩的原因。 編寫代碼后,單擊“運行”模塊,然后Python Shell重新啟動,不會引發語法錯誤。 但是,當我測試代碼時,它返回一個空列表。 代碼在這里。 您能告訴我我的算法有什么問題嗎? 我真的很失望。

在此先感謝您的幫助!

def assing_boundaries(L):

    """ (List of str -> List of str)

    Assings the syllable boundaries for a given string of Syllable      Templates, with the output meeting the two conditions given below:
Condition1: item[2:] != 'CC'
Condition2: item[:-2] !='C.C'

    >>> assing_boundaries(['CVCCVCC', 'CVCCV:C'])
    ['CVC.CVCC', 'CVC.CV:C']   
    """


    boundary = []
    i = 0

    for item in L:
        for char in item:
            for i in range(len(item) - 1):
                if item[i] == 'CC':
                    char = 'C.C'
                    i = i + 1
                    boundary = boundary.append(item)


    return boundary
  1. 您的循環是錯誤的。 您可以測試if item[i] == 'CC' ,但item[i]只能是單個字符。
  2. boundary = boundary.append(item)是錯誤的,因為.append返回None 只需執行boundary.append(item)

更簡單的是,為什么不只return [item.replace('CC', 'C.C') for item in L]呢? 您可以調整列表理解以根據需要過濾列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM