繁体   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