1

Let's say that i have a input in my program where the user writes a text. I save this text as a long string and I later convert it to a list with all the words in throught the split()-function.

I also have a list with several word as elements. If the words in this list exist in the first list, the words should be deleted in the first list. HOW can i code this?

I used the .remove()-function, however if the word appears two times in different places, only the first was removed...

2 Answers 2

2

You can use list comprehention to filter out word, like this

list1, rep = "Welcome to Stack Overflow, to learn".split(), "to"
list1 = [word for word in list1 if word != rep]
print list1

Or you can use filter function, like this

list1 = filter(lambda word: word != rep, list1)

If you want to remove all the words which are not needed, you can do it like this

list1, words = "Welcome to Stack Overflow, to learn".split(), {"to", "Stack"}
list1 = [word for word in list1 if word not in words]
8
  • the problem here would be if a word i'd like to delete is followed by a , or . or ! or ? it wouldn't be deleted. How could i fix this<+ Dec 28, 2013 at 10:11
  • @user3141639 Then you might want to use regular expressions. Dec 28, 2013 at 10:13
  • So those signs should be deleted from the beginning in the list? Dec 28, 2013 at 10:16
  • Does your example above only work for the string "to"? how can i use the rep-thing with a already prepared list with words that should be deleted Dec 28, 2013 at 10:18
  • @user3141639 Your original question stated only one word :( Dec 28, 2013 at 10:20
0
l = ["A", "super", "cool", "list", "of", "words"]
words = ["list", "words"]

If you want to keep the same list object:

l[:] = (w for w in l if w not in words)

Otherwise you can use a simple list comprehension:

l = [w for w in l if w not in words]

Or using filter:

l = list(filter(lambda w: w not in words, l))

Or:

l[:] = filter(lambda w: w not in words, l)
7
  • Thank you, it worked almost perfect. The only problem I have now is that if the word from words is behind a , or . or ! or ? it won't be deleted since the actual string in the first list will be "Super." or "cool?". How would you recommend to deal with this? Should I try to delete these directly in my earlier stages of my program? Dec 28, 2013 at 10:38
  • If I understand you correctly, you cool try regexp replacements. Or you strip the punctuation when you do the comparison. If you could provide an actual input and the intended output, that would be easier. Dec 28, 2013 at 10:42
  • I cannot post my code directly here unfortunately. I could try to clarify what i mean. I have a program which takes a written text as input. Lets say the text is: "My cool brother is really cool. He loves apples which is cool. Do you think that is cool?" since my task is to do some calculations with the text (number of words/senteces etc.) I made two lists of this string. One where the string was split at . or ! or ?. One where it was split at spaces in order to separate each word. The problem now is that when separated with spaces, the . or ! or ? follows the string. Dec 28, 2013 at 10:48
  • So the list for spliting at spaces would be. list = ["My", "Cool", "brother", "is", "really", "cool."] Dec 28, 2013 at 10:49
  • SO, everytime a sentece end. The ./!/? is followed. And since the list om search through with my other list with words that should be deleted is the list i printed above, a problem occurs when i'd like to delete the word "cool". Dec 28, 2013 at 10:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.