Application of python lists and iterables

hello, the code below is meant to iterate through a string argument of a function, the code is then supposed to iterate and pick out the words which are created from characters that are in ascii_letters and ignoring all other characters, a few of the doc tests demonstrate the required outcome. However the code does not give the required results but instead return …please help in debugging this code.

import ascii_letters
def extract_words(text):
    """Return the words in a tweet, not including punctuation.

    >>> extract_words('anything else.....not my job')
    ['anything', 'else', 'not', 'my', 'job']
    >>> extract_words('i love my job. #winning')
    ['i', 'love', 'my', 'job', 'winning']

    lst=[]
    def count_index(i):
        a=i
        while a<len(text):
            if text[a] in ascii_letters:
                a=a+1
            else:
                a = a+1
        if a>=len(text):
            return lst
        else:
            return conc_list(i,a)
    def conc_list(i,a):
        what=text[i:a]
        
        lst=lst.append(what)
        return count_index(a)


    return count_index(0)

Can you give more details about in what ways “the code does not give the required results”?

Hie , the following is an error message when i run the code .

File "C:\Users\Perez\downloads\trends\trends\trends.py", line 109, in extract_words
Failed example:
    extract_words('@(cat$.on^#$my&@keyboard***@#*')
Expected:
    ['cat', 'on', 'my', 'keyboard']
Got:
    []
A doctest example failed for extract_words.

Can you explain how you intend for this function to work? I don’t think I understand the process you are trying to made the computer follow. (describe in words, not code, please!)

okay, a string argument is passed into the extract words function. the extract_words function returns a call to the count_index function that starts to iterate through the string from the zero index, if the character at each index is in ascii_letters , the iteration continues, hence the reason for the while loop.

the variable i is a counter that will store the initial index value of when iteration began.
then the variable a is a counter that will keep track of the last index value for which the iteration continued before reaching a character that is not in ascii_letters.

once the iteration is disturbed or interrupted the first and last index values of the undisturbed iteration will be used as arguments to slice the text within those parameters and enter the slice into the list as an element in the conc_list function,

then the whole process recurrs until the whole text has been iterated through

I think I get your intended flow of logic, thanks.

I’m suspicious here. Why do the exact same thing no matter if you have a letter or not?

I think you need to consider what should happen if the last thing in text is a letter.

Is the letter at index a included or not here? Good to clarify which you want/need.

One issue is that this isn’t how you use .append()

Is it not that the iteration will stop once a the index at a character is not in ascii_letters, therefore slice[i:a] will return all characters from index i to index (a-1) because a will be out of bound so to speak…

or am i wrong??

i do get your point about what i did here, it makes sense what you are saying

How come?? What is wrong about using append() in that way

What is the return value of the .append() method?

i think that , for example, lst.append(item) method should pass into the list the item as an element

Right, the item you want to append to the list is the argument passed into .append()

But you have lst = lst.append(item). This says to overwrite lst with the return value of .append(). So what value does .append() return/make?

this is an attempt to correct some of the things you highlighted, did i make it worse or better :grimacing:

 lst=[]
    def count_index(i):
        a=i
        if a<len(text):
            if text[a] in ascii_letters:
                a=a+1
            if text[a] not in ascii_letters:
                return conc_list(i,a)
        if a>=len(text):
            return lst

    def conc_list(i,a):
        what=text[i:a]
        if what=='':
            return count_index(a)
        else:
            lst=lst.append(what)
            return count_index(a)


    return count_index(i=0)

This still isn’t going to work.


I think you need your while loop to make a bigger.

if i am understanding you correctly, you are implying that if i use this

i am basically changing/overwriting list to whatever value that list.append(item) gives me at any stage in the recursive calls, so lst is being overwritten during every single recursive cycle…

is that it ??

please do elaborate more…

do i run away from using .append() method entirely or change

to just lst.append(what)

what do you mean by making my while loop bigger??

I like this idea.

You removed the while loop. You should put it back.

lst=[]
    def count_index(i):
        a=i
        while a<len(text):
            if text[a] in ascii_letters:
                a=a+1
            if text[a] not in ascii_letters:
                return conc_list(i,a)
        if a>=len(text):
            return lst

    def conc_list(i,a):
        what=text[i:a]
        if what=='':
            return count_index(a)
        else:
            lst.append(what)
            return count_index(a)


    return count_index(i=0)

So that should change things a little bit. Hows it working?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.