Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurrences of given words per each list in a tuple of lists

I have a list of tokenized sentences and I want to count the collective occurrence of several words: e.g.:

example_list = (['hey', 'there', 'you', 'how', 'are', 'you'],
                ['i', 'am', 'fine', 'how', 'about', you],
                ['i', 'am', 'good'])

Now I want to count how many times the following words occur in each list and append the score in a list

score = []
test = ['hey', 'you']

I try the following code:

for i in range(len(test)):
   for j in range(len(example_list)):
       score1.append(example_list[j].count(test[i]))

and get the output of:

[1, 0, 0, 2, 1, 0]

whereas I want an output of:

[3, 1, 0]

any ideas?

like image 358
apol96 Avatar asked Oct 16 '25 06:10

apol96


2 Answers

You could use sum inside a list comprehension:

example_list = (['hey', 'there', 'you', 'how', 'are', 'you'],
                ['i', 'am', 'fine', 'how', 'about', 'you'],
                ['i', 'am', 'good'])



test = ['hey', 'you']


score = [sum(s in test for s in lst) for lst in example_list]
print(score)

Output

[3, 1, 0]

Consider using a set if test is large enough.

like image 167
Dani Mesejo Avatar answered Oct 18 '25 18:10

Dani Mesejo


You can use Counter for this task:

from collections import Counter


counters = [Counter(l) for l in example_list]
occurrences = [sum([c[word] for word in test if word in c]) for c in counters]

print(occurrences) # [3, 1, 0]
like image 36
Gabio Avatar answered Oct 18 '25 18:10

Gabio