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 4006548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:31:41+00:00 2026-05-20T08:31:41+00:00

I need to make a program that prints out the frequency of letters in

  • 0

I need to make a program that prints out the frequency of letters in a text
file and compares that frequency with that of another in python.

So far I am able to print the number of times a letter occurs, but the
percentage frequency I get is wrong. I think it is because I need my program to count only
the number of letters in the file by removing all the spaces and other
characters.

def addLetter (x):
    result = ord(x) - ord(a)
    return result


#start of the main program
#prompt user for a file

while True:
    speech = raw_input("Enter file name:")

    wholeFile = open(speech, 'r+').read()
    lowlet = wholeFile.lower()
    letters= list(lowlet)
    alpha = list('abcdefghijklmnopqrstuvwxyz')
    n = len(letters)
    f = float(n)
    occurrences = {}
    d = {}


    #number of letters
    for x in alpha:
        occurrences[x] = letters.count(x)
        d[x] =(occurrences[x])/f
    for x in occurrences:
        print x, occurrences[x], d[x]

This is the output

Enter file name:dems.txt
a 993 0.0687863674148
c 350 0.0242449431976
b 174 0.0120532003325
e 1406 0.0973954003879
d 430 0.0297866444999
g 219 0.015170407315
f 212 0.0146855084511
i 754 0.0522305347742
h 594 0.0411471321696
k 81 0.00561097256858
j 12 0.000831255195345
m 273 0.0189110556941
l 442 0.0306178996952
o 885 0.0613050706567
n 810 0.0561097256858
q 9 0.000623441396509
p 215 0.0148933222499
s 672 0.0465502909393
r 637 0.0441257966196
u 305 0.021127736215
t 1175 0.0813937378775
w 334 0.0231366029371
v 104 0.00720421169299
y 212 0.0146855084511
x 13 0.000900526461624
z 6 0.000415627597672
Enter file name:

The program does print in columns, but I’m not really sure how to display that here.

the frequency for “a” should be .0878

  • 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-05-20T08:31:41+00:00Added an answer on May 20, 2026 at 8:31 am

    You could use the translator recipe to drop all characters not in alpha.
    Since doing so makes letters contain nothing but characters from alpha, n is now the correct denominator.

    You could then use a collections.defaultdict(int) to count the occurrences of the letters:

    import collections
    import string
    
    def translator(frm='', to='', delete='', keep=None):
        # Python Cookbook Recipe 1.9
        # Chris Perkins, Raymond Hettinger
        if len(to) == 1: to = to * len(frm)
        trans = string.maketrans(frm, to)
        if keep is not None:
            allchars = string.maketrans('', '')
            # delete is expanded to delete everything except
            # what is mentioned in set(keep)-set(delete)
            delete = allchars.translate(allchars, keep.translate(allchars, delete))
        def translate(s):
            return s.translate(trans, delete)
        return translate
    
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    keep_alpha=translator(keep=alpha)
    
    while True:
        speech = raw_input("Enter file name:")
        wholeFile = open(speech, 'r+').read()
        lowlet = wholeFile.lower()
        letters = keep_alpha(lowlet)
        n = len(letters)
        occurrences = collections.defaultdict(int)    
        for x in letters:
            occurrences[x]+=1
        for x in occurrences:
            print x, occurrences[x], occurrences[x]/float(n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to make a program that looks through a mailbox
I need to make a program that adds long integers without using the biginteger
I need to make the program which have one form that contains PNG image
For my project, I need to make a program that takes 10 numbers as
We need to make some small program for school that rolls 5 dices and
I need to write a program that uses brute-force method to find out how
I'm trying to make a program that takes in one argument, a file, and
I tried to make a short program that works out the famous Drake equation.
I need to make a program that accepts no less than 2 and no
I need to make a java program with a security and a login system

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.