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...