Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:12:18+00:00 2026-06-04T18:12:18+00:00

I have python 2.7 code to square the values in a FreqDist (i.e. an

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:12:19+00:00Added an answer on June 4, 2026 at 6:12 pm
    lst = [2, 1, 1, 1]
    

    Using a generator expression:

    sum(i**2 for i in lst)
    

    gives

    7
    

    Alternatively, list comprehension also works:

    sum([i**2 for i in lst])
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python code like below to search all the English names: a
I have this python code: import sys import dpkt f = file(pcaop.Pcap) pcap =
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have some python code that looks like this: return [x.doSomething() for x in
I have a python code something like this file_out.write(str(i).strip()+\t+str(dict1[i]).strip()+\n) But when i try to
I have this Python code that I found online and would like to know
I have this python code for opening a .cfg file, writing to it and
I have a python code like this. File named mymodule.py class MyBase(object): pass File
I have Python code that pulls info from a sqlite database and then write
I have python code with C libraries, I use SWIG to plug it all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.