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

  • Home
  • SEARCH
  • 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 8540867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:41:50+00:00 2026-06-11T11:41:50+00:00

I have a small python script that prints the 10 most frequent words of

  • 0

I have a small python script that prints the 10 most frequent words of a text document (with each word being 2 letters or more) and I need to continue the script to print the 10 most INfrequent words in the document as well. I have a script that is relatively working, except the 10 most infrequent words it prints are numbers (integers and floaters) when they should be words. How can I iterate ONLY words and exclude the numbers? Here is my full script:

# Most Frequent Words:
from string import punctuation
from collections import defaultdict

number = 10
words = {}

with open("charactermask.txt") as txt_file:
    words = [x.strip(punctuation).lower() for x in txt_file.read().split()]

counter = defaultdict(int)

for word in words:
  if len(word) >= 2:
    counter[word] += 1

top_words = sorted(counter.iteritems(),
                    key=lambda(word, count): (-count, word))[:number] 

for word, frequency in top_words:
    print "%s: %d" % (word, frequency)


# Least Frequent Words:
least_words = sorted(counter.iteritems(),
                    key=lambda (word, count): (count, word))[:number]

for word, frequency in least_words:
    print "%s: %d" % (word, frequency)

EDIT: The end of the document (the part under the # Least Frequent Words comment) is the part that needs fixing.

  • 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-11T11:41:51+00:00Added an answer on June 11, 2026 at 11:41 am

    You’re going to need a filter — change the regex to match however you want to define a “word”:

    import re
    alphaonly = re.compile(r"^[a-z]{2,}$")
    

    Now, do you want the word frequency table to not include numbers in the first place?

    counter = defaultdict(int)
    
    with open("charactermask.txt") as txt_file:
        for line in txt_file:
            for word in line.strip().split():
              word = word.strip(punctuation).lower()
              if alphaonly.match(word):
                  counter[word] += 1
    

    Or do you just want to skip over the numbers when extracting the least-frequent words from the table?

    words_by_freq = sorted(counter.iteritems(),
                           key=lambda(word, count): (count, word))
    
    i = 0
    for word, frequency in words_by_freq:
        if alphaonly.match(word):
            i += 1
            sys.stdout.write("{}: {}\n".format(word, frequency))
        if i == number: break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script running on a small server that is called in
I have a small python cgi script that accepts an image upload from the
I currently have a small Python script that I'm using to spawn multiple executables,
I have a small Python script that I need to modify because the format
I have a python script that is essentially a small socket server that does
I have a small python script that creates a graph of data pulled from
I have a small in-house Python script for Linux that creates a /home/user/environ_script.sh file
I have a small python script which draws some turtle graphics. When my script
I have a small GTK python application that imports a package (Twisted) that may
I would like to build a small python script that basicaly does the reverse

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.