Count the number of words in a string list

How to count how many times a word appears in a list of strings?

For instance:

['This is a sentence', 'This is another sentence'] 

and the result for the word "sentence" is 2

-2
source share
2 answers

Use the collections.Counter() object and separate the words into spaces. You might also want to smooth your words and remove punctuation marks:

 from collections import Counter counts = Counter() for sentence in sequence_of_sentences: counts.update(word.strip('.,?!"\'').lower() for word in sentence.split()) 

or perhaps use a regular expression that matches only word characters:

 from collections import Counter import re counts = Counter() words = re.compile(r'\w+') for sentence in sequence_of_sentences: counts.update(words.findall(sentence.lower())) 

You now have a dictionary of counts with word counting.

Demo:

 >>> sequence_of_sentences = ['This is a sentence', 'This is another sentence'] >>> from collections import Counter >>> counts = Counter() >>> for sentence in sequence_of_sentences: ... counts.update(word.strip('.,?!"\'').lower() for word in sentence.split()) ... >>> counts Counter({'this': 2, 'is': 2, 'sentence': 2, 'a': 1, 'another': 1}) >>> counts['sentence'] 2 
+9
source

You can do what you want quite easily, with a little regex and a dictionary.

 import re dict = {} sentence_list = ['This is a sentence', 'This is a sentence'] for sentence in sentence_list: for word in re.split('\s', sentence): # split with whitespace try: dict[word] += 1 except KeyError: dict[word] = 1 print dict 
+2
source

Source: https://habr.com/ru/post/1502404/


All Articles