In: Computer Science
Python Programming Problem:
If I have to separate lists, one contains a large string of paragraphs of words, and one contains just words, how can i iterate the words in my second list and compare it to my paragraph list to see how many times that word has occurred?
List1 = ['paragraph.......']
List2 = ['words', 'words', 'words'......]
these are just minimal examples
how do i approach this problem?
apprently numpy helps with processing time for something like this? cuz the lists could get quite big
You need to use numpy's char.count () function, it returns the number of times a word is present in a paragraph Below is a naive example just to demonstrate how to use it. ================================================================================== import numpy as np paragraphs = np.array( ['the fox jumped and the fox drowned and the fox survived', 'the brown fox and the black fox fought in the mud.']) words = np.array(['and', 'fox', 'the']) for word in words: for paragraph in paragraphs: count = np.char.count(paragraph, word) print(paragraph, 'contains', count, '\"', word, "\"", 'word(s).')
======================================================================