Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove words from a list in python

Tags:

python-3.x

I have done my code this far but it is not working properly with remove()..can anyone help me..

'''
Created on Apr 21, 2015

@author: Pallavi
'''
from pip._vendor.distlib.compat import raw_input
print ("Enter Query")
str=raw_input()  

fo = open("stopwords.txt", "r+")
str1 = fo.read();
list=str1.split("\n");
fo.close()
words=str.split(" ");
for i in range(0,len(words)):
    for j in range(0,len(list)):
        if(list[j]==words[i]):
            print(words[i])
            words.remove(words(i))

Here is the error:

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
like image 589
pallavi gupta Avatar asked Apr 21 '15 11:04

pallavi gupta


People also ask

How do I remove a string from a list in Python?

pop() method to remove a string from a list, e.g. my_list. pop(0) . The list. pop() method will remove the string at the specified position and will return the removed value.

Can you remove items from a list in Python?

Delete an element from a list using the del. The Python del statement is not a function of List. Items of the list can be deleted using the del statement by specifying the index of the item (element) to be deleted.


3 Answers

The errors you have (besides my other comments) are because you're modifying a list while iterating over it. But you take the length of the list at the start, thus, after you've removed some elements, you cannot access the last positions.

I would do it this way:

words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)

An even more pythonic way using list comprehensions:

new_words = [word for word in words if word not in stopwords]
like image 144
Francis Colas Avatar answered Sep 28 '22 06:09

Francis Colas


As an observation, this could be another elegant way to do it:

new_words = list(filter(lambda w: w not in stop_words, initial_words))
like image 31
Sorin Dragan Avatar answered Sep 28 '22 07:09

Sorin Dragan


''' call this script in a Bash Konsole like so:    python  reject.py
    purpose of this script: remove certain words from a list of words ,
    e.g. remove invalid packages in a request-list using 
    a list of rejected packages from the logfile, 
    say on https://fai-project.org/FAIme/#
    remove trailing spaces e.g. with KDE Kate in wordlist like so:

kate: remove-trailing-space on; BOM off;
'''
with open("rejects", "r+")       as fooo   :
    stwf    = fooo.read()
toreject    = stwf.split("\n")

with open("wordlist", "r+")      as bar    :
  woL       = bar.read()
words       = woL.split("\n")

new_words = [word for word in words if word not in toreject]
with open("cleaned", "w+")       as foobar :
    for ii in new_words:
        foobar.write("%s\n" % ii)
like image 20
dotbit Avatar answered Sep 28 '22 07:09

dotbit