简体   繁体   中英

Removing elements from list in python

I have a list with this shape:

temp5=[]
for i in range(0,len(df)):
   temp5.append(df['text'][i].split())
df['each']=temp5
df['each']

and the result is like this:

在此处输入图片说明

now I want to remove some elements of the previous list. I want to check if each word of the previous list is similar to the following list, remove it from it. the second list is like this:

stopwords = open('stop_words.txt','r').read().split('\n')
print(stopwords)

在此处输入图片说明

now I wrote this code to delete the same words of each list from the first one. but all I receive is NONE. Could you please help me with it?

for k in range(0,len(df)):
    for j in df['each'][k][:]:
        for f in stopwords:
            if f==j:
                temp6.append(df['each'][k][:].remove(f))
                print(temp6)

As mentioned in the comments, remove method removes inplace, but if you want something more 'pythonic', the working code would be

temp5=[]
for i in range(0,len(df)):
    temp5.append([x for x in df['text'][i].split() if x not in stopwords])

using the list comprehension as mentioned eg in this question , which creates the filtered list. Or, if you insist on using the original dataframe as input, it would be something like

temp5=[]
for i in range(0,len(df)):
    temp5.append([x for x in df['each'][i] if x not in stopwords])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM