I have python 2.7 code to square the values in a FreqDist (i.e. an NLTK frequency distribution), and the sum all the squares.
For example, from this:
You should get:
2*2 + 1*1 + 1*1 + 1*1 = 7
This works for me, but I was wondering whether there was a “better” way to do it than this:
for word, frequency in t.freq_dist.iteritems():
total += frequency*frequency
I’m asking because I need to then loop through freq_dist again for something else; right after this code, so I figured it’s not good practice to have to loop through it twice if there’s a better way…
Using a generator expression:
gives
Alternatively, list comprehension also works:
If you don’t need the squared values for some purpose later, then the generator expression is a better choice as it creates the values only once on demand whereas list comprehension creates the whole list in memory. For more information see this SO question comparing list comprehension vs generators.