嵌套列表理解以实现嵌套列表

时间:2019-04-18 18:59:19

标签: python list nltk list-comprehension

import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords

list1 =['This is text','This is another text']
stp = stopwords.words('English')

lower_token =  [t.lower().split() for t in list1]


new2=[]
for list in lower_token:
  new1=[]
  for word in list:
    if word not in stp:
      new1.append(word)
  new2.append(new1)

new2
  
    

[[['text'],['another','text']]

  

在上面的条件循环中,我尝试将split the text分成两个列表,然后排除stp列表中出现的所有单词。尽管我可以使用for循环来实现结果,但是我很想使用列表理解来实现相同的结果,但是我做不到。

这是我使用列表理解的不成功尝试

[word for list in lower_token for word in list if word not in stp]

2 个答案:

答案 0 :(得分:3)

您还需要将内部理解内容也包含在列表中。

[[word for word in list if word not in stp] for list in lower_token]

答案 1 :(得分:3)

您非常接近,您也只需将内部列表理解放在方括号中。这也使它更具可读性。

[[word for word in txt.lower().split() if word not in stp] for txt in list1]
相关问题