简体   繁体   中英

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'. 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. 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.
  2. boundary = boundary.append(item) is wrong, because .append returns None . Just do boundary.append(item) .

Simpler yet, why not just do return [item.replace('CC', 'C.C') for item in L] instead? You can tweak the list comprehension to filter the list as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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