LiverToll92
LiverToll92

Reputation: 97

Deleting elements in list of list using list comprehensions(Python)

I have the following data:

[['The',
  'Fulton',
  'County',
  'Grand',
  'Jury',
  'said',
  'Friday',
  'an',
  'investigation',
  'of',
  "Atlanta's",
  'recent',
  'primary',
  'election',
  'produced',
  '``',
  'no',
  'evidence',
  "''",
  'that',
  'any',
  'irregularities',
  'took',
  'place',
  '.'],
 ['The',
  'jury',
  'further',
  'said',
  'in',
  'term-end',
  'presentments',
  'that',
  'the',
  'City',
  'Executive',
  'Committee',
  ',',
  'which',
  'had',
  'over-all',
  'charge',
  'of',
  'the',
  'election',
  ',',
  '``',
  'deserves',
  'the',
  'praise',
  'and',
  'thanks',
  'of',
  'the',
  'City',
  'of',
  'Atlanta',
  "''",
  'for',
  'the',
  'manner',
  'in',
  'which',
  'the',
  'election',
  'was',
  'conducted',
  '.']]

So I have a list that consistst of 2 other list(in my case I have 50000 lists in one big list). I want to delete all punctuation and stopwords like "the", "a" "of" etc.

Here is what I have coded:

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

punct = list(string.punctuation)
punct.append("``")
punct.append("''")
stops = set(stopwords.words("english")) 

res = [[word.lower() for word in sentence if word not in punct or word.lower() in not stops] for sentence in dataset] 

But it returns me the same list of lists that I initially had. What is wrong with my code?

Upvotes: 1

Views: 50

Answers (3)

FredrikHedman
FredrikHedman

Reputation: 1253

Assumning it would be ok to update the stops this is an alternative that avoids the 2-level comprehension

import string
import nltk
from nltk.corpus import stopwords


dataset = [
  ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an',
   'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election',
   'produced', '``', 'no', 'evidence', "''", 'that', 'any', 'irregularities',
   'took', 'place', '.'],
  ['The', 'jury', 'further', 'said', 'in', 'term-end', 'presentments',
   'that', 'the', 'City', 'Executive', 'Committee', ',', 'which', 'had',
   'over-all', 'charge', 'of', 'the', 'election', ',', '``', 'deserves',
   'the', 'praise', 'and', 'thanks', 'of', 'the', 'City', 'of', 'Atlanta',
   "''", 'for', 'the', 'manner',
   'in', 'which', 'the', 'election', 'was', 'conducted', '.']
  ]

nltk.download('stopwords')

punct = list(string.punctuation)
punct.append("``")
punct.append("''")

stops = set(stopwords.words("english"))

# Union of punct and stops
stops.update(punct)
res1 = [[word for word in sentence if word.lower() not in stops]
        for sentence in dataset]

# Alternative solution that avoids an explict 2-level list comprehension
def filter_the(sentence, stops):
    return [word for word in sentence if word.lower() not in stops]


res2 = [filter_the(sentence, stops) for sentence in dataset]


print(res1 == res2)

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49813

Since punct and stops do not over lap, every word will either not be in one or the other (or possibly both); you want to test for words that are not in both.

Upvotes: 2

neverwalkaloner
neverwalkaloner

Reputation: 47364

You shoud use and unstead of or:

res = [[word.lower() for word in sentence if word not in punct and word.lower() not in stops] for sentence in dataset]

Otherwise you get all elements since they are not exist at leatst in one of stops or punct list.

Upvotes: 2

Related Questions