[Tutor] deleting elements out of a list.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Sat Jun 15 00:51:23 EDT 2019


All,

 

I am not sure how to tackle this issue. I am using Windows 10 and Python 3.6
from Activestate. 

 

I have a list of x number of elements. Some of the elements are have similar
words in them. For example:

 

Dog food Pal

Dog Food Pal qx1323 

Cat food kitty

Absolute cleaning inv123

Absolute Domestic cleaning inv 222

Absolute d 3333

Fitness first 02/19

Fitness first

 

I wish to remove duplicates. I could use the collection.Count method. This
fails due to the strings are not unique, only some of the words are. My
thinking and is only rough sudo code as I am not sure how to do this and
wish to learn and not sure how to do without causing gtraceback errors. I
want to delete the match pattern from the list of strings. Below is my
attempt and I hope this makes sense. 

 

 

description = load_files() # returns a list

for text in description:

    words = text.split()

    for i in enumerate(words):

        Word = ' '.join(words[:i])

        print (word)

        answer = input('Keep word?')

        if answer == 'n':

            continue 

        for i, v in enumerate(description):

            if word in description[i]:

                description.pop[i]

        

 

The initial issues I see with the above is the popping of an element from
description list will cause a error. If I copy the description list into a
new list. And use the new list for the outer loop. I will receive multiple
occurrences of the same text. This could be addressed by a if test. But I am
wondering if there is a better method. 2nd code example:

 

description = load_files() # returns a list

search_txt = description.copy() # I have not verify if this is the right
syntax for the copy method.]

for text in search_txt:

    words = text.split()

    for i in enumerate(words):

        Word = ' '.join(words[:i])

        print (word)

        answer = input('Keep word (ynq)?')

        if answer == 'n':

            continue 

        elif answer = 'q':

            break

        for i, v in enumerate(description):

            if word in description[i]:

                description.pop[i]

            

 

Any improvements?

 

Sean 



More information about the Tutor mailing list