0

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
4

1 回答 1

0
  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.

于 2013-03-31T02:57:16.727 回答