[Tutor] deleting elements out of a list.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Sat Jun 15 20:51:07 EDT 2019


Allan,

 

I will respond to your reply in the other thread. As I want to respond to everyone in one email. Thanks for the clarification on the questions or information I did not provide. 101 troubleshooting assistance. Do not assume others know what you know. 😊

 

-----Original Message-----
From: Tutor <tutor-bounces+mhysnm1964=gmail.com at python.org> On Behalf Of Alan Gauld via Tutor
Sent: Sunday, 16 June 2019 7:25 AM
To: tutor at python.org
Subject: Re: [Tutor] deleting elements out of a list.

 

On 15/06/2019 05:51,  <mailto:mhysnm1964 at gmail.com> mhysnm1964 at gmail.com wrote:

 

Caveat: I'm picking this up late in the day and only had a cursory look at it, so may be missing some critical insight...

 

> I have a list of x number of elements. Some of the elements are have 

> similar words in them. For example:

 

Define "similar".

It implies not identical. What is different? What makes them similar? Every time you introduce vague inequalities you imply the need for some kind of intelligent function that removes the ambiguity and vagueness. it  definitively says that these two items are similar or not similar.

 

Can you write such a function? If so the problem should become relatively simple.

 

 

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

 

So what would the output look like if the above is the input?

My guess of what you want is:

 

qx1323

Cat kitty

Domestic

d 3333

02/19

 

Is that right?

Or is my idea of similar and duplicate different to yours?

 

> I could use the collection.Count method. This fails due to the strings 

> are not unique, only some of the words are.

 

Sorry, I can't understand that. It makes no sense to me.

You need to define strings and words in this context

 

> description = load_files() # returns a list

 

A list of what? characters, words, lines?

 

> for text in description:

>     words = text.split()

>     for i in enumerate(words):

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

 

This is weird. enumerate returns tuples which you assign to i.

But then you use i in a slice opertion. But slice expects an integer.

 

 

>         print (word)

>         answer = input('Keep word?')

>         if answer == 'n':

>             continue 

>         for i, v in enumerate(description):

>             if word in description[i]:

>                 description.pop[i]

 

Without any clue what the description data looks like we can't really decipher what the code above does.

 

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

 

I'm not sure thats true but it denends on what description looks like.

 

> 

> description = load_files() # returns a list

> 

> search_txt = description.copy()

> 

> 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]

 

The usual way to remove things from a list is to create a new list using a filter

 

newlist = filter(test_function, oldlist)

 

or a list comprehension

 

newlist = [item for item in oldlist if test_function(item)]

 

Which brings us back to the beginning. Can you write a test function that unambiguously defines what needs to be removed?

 

 

--

Alan G

Author of the Learn to Program web site

 <http://www.alan-g.me.uk/> http://www.alan-g.me.uk/

 <http://www.amazon.com/author/alan_gauld> http://www.amazon.com/author/alan_gauld

Follow my photo-blog on Flickr at:

 <http://www.flickr.com/photos/alangauldphotos> http://www.flickr.com/photos/alangauldphotos

 

 

_______________________________________________

Tutor maillist  -   <mailto:Tutor at python.org> Tutor at python.org

To unsubscribe or change subscription options:

 <https://mail.python.org/mailman/listinfo/tutor> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list