简体   繁体   English

我在使用Python中的音节边界算法时遇到麻烦

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

could you help me please? 请问你能帮帮我吗?

I am trying to desing an algorithm that assings the syllable boundaries for any given string of consonanats and vowels 'CVCCVCCVCC'. 我正在尝试设计一种算法,该算法可对任何给定的辅音和元音“ CVCCVCCVCC”字符串的音节边界进行评估。 These strings are stored in a list that may run into hundred of thousands or even million of itmes in length. 这些字符串存储在一个列表中,该列表的长度可能达到数十万甚至上百万个。 However, it seems that I did not understand handling lists properly and that is why I am getting this trouble. 但是,似乎我不了解如何正确地处理列表,这就是我遇到此麻烦的原因。 After composing the code, I click "Run" the module, and Python shell restarts, raising no syntax error. 编写代码后,单击“运行”模块,然后Python Shell重新启动,不会引发语法错误。 However, when I test the code, it returns an empty list. 但是,当我测试代码时,它返回一个空列表。 The code is here. 代码在这里。 Could you please tell me what is the wrong with my algorithm? 您能告诉我我的算法有什么问题吗? I am getting really disappointed. 我真的很失望。

Thanks in advance for kind help! 在此先感谢您的帮助!

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. Your loop is wrong. 您的循环是错误的。 You test if item[i] == 'CC' , but item[i] can only ever be a single character. 您可以测试if item[i] == 'CC' ,但item[i]只能是单个字符。
  2. boundary = boundary.append(item) is wrong, because .append returns None . boundary = boundary.append(item)是错误的,因为.append返回None Just do boundary.append(item) . 只需执行boundary.append(item)

Simpler yet, why not just do return [item.replace('CC', 'C.C') for item in L] instead? 更简单的是,为什么不只return [item.replace('CC', 'C.C') for item in L]呢? You can tweak the list comprehension to filter the list as needed. 您可以调整列表理解以根据需要过滤列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM