Good afternoon, I'm just starting to learn python and, in particular, I am sorting out the task of finding the occurrence of certain words in a sentence. How the most beautiful in terms of python 2.7 can describe the text below?

import numpy as np sfArray=np.zeros((len(sfSentences),len(sfDict))) print len(sfSentences) i=0 for Sentence in sfSentences: j=0 for Word in sfDict: Wordlist=Sentence.split() sfArray[i,j]=Wordlist.count(Word) j=j+1 i=i+1 print sfArray 

I haven’t really figured out the lambda function and similar python constructions, so I’ll be happy with any advice

  • one
    The beauty of such libraries as NumPy, Pandas, etc. in the "vectorized" approach - i.e. in the use of off-the-shelf functions (often written in C / Cython and optimized for performance and memory usage) instead of chains. The most beautiful, in my opinion, will be to use the NLTK library, which was created for just such things. Here is an example of finding the most popular words in a text that NLTK and Pandas use. - MaxU
  • @MaxU Yes, thank you, I will smoke NLTK - it looks interesting - Sasha Che

0