Question

For this problem, you will write a program to form a frequency table of the letters...

For this problem, you will write a program to form a frequency table of the letters in a text file, that is, how many times each letter appears in the file In Python 3.7.

Such a frequency table might be useful for compressing a text file. Because different letters appear with different frequencies, we can compress a file by using shorter codes for common letters and longer codes for letters that appear less frequently.

Dictionaries provide an elegant way to generate a frequency table:

>>> letter_counts = {}

>>> for letter in "Mississippi":

...     letter_counts[letter] = letter_counts.get(letter, 0) + 1

...

>>> letter_counts

{'M': 1, 's': 4, 'p': 2, 'i': 4}

We start with an empty dictionary. For each letter in the string, we find the current count (possibly zero) and increment it. At the end, the dictionary contains pairs of letters and their frequencies.

It might be more appealing to display the frequency table in alphabetical order. We can do that with the items and sort methods:

>>> letter_items = list(letter_counts.items())

>>> letter_items.sort()

>>> print(letter_items)

[('M', 1), ('i', 4), ('p', 2), ('s', 4)]

Notice in the first line we had to call the type conversion function list. That turns the promise we get from items into a list, a step that is needed before we can use the list’s sort method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

python code:

letter_counts = {}

for letter in "Mississippi":
letter_counts[letter] = letter_counts.get(letter, 0) + 1

print(letter_counts)

letter_items = list(letter_counts.items())

letter_items.sort()

print(letter_items)

f= open("C:/Users/Apple/Desktop/frequencyTable.txt","w+") #open file

for i in letter_items:
f.write(str(i)) #write to file
f.close() #close

code:

Le freq.ру-С:/Python34/freq.py (3.4.3) File Edit Format Run Options Window Help letter c unts = {} for letter in Mississippi

output:

ype copyright, cr editsorlicense or more inio RESTART S>

//for any clarification, do comments..

Add a comment
Know the answer?
Add Answer to:
For this problem, you will write a program to form a frequency table of the letters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1 This program is about dictionaries. We want to use a dictionary to store frequency...

    Problem 1 This program is about dictionaries. We want to use a dictionary to store frequency count of each letter in a string. Write a Python program to do the following: (a) Ask the user to enter a string. Convert all letters to uppercase. Count the frequency of each letter in the string. Store the frequency counts in a dictionary. You should count letters only. Do not count any other characters such as digits and space. Display the dictionary. (b)...

  • Write a program that counts the frequency of each letter of the alphabet as they occur...

    Write a program that counts the frequency of each letter of the alphabet as they occur in a text contained in a text file. The program should read all the characters from a file and tally the frequency for each letter of the alphabet (case-insensitive), along with white space and total character count. Your program's data structure will eventually contain the 26 numbers that will represent the frequencies of occurrence of all 26 letters of the alphabet in that file....

  • java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...

    java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text file (a novel or a report, or just a sequence of letters or symbols), by reading the file into a byte array, convert to a string, and then scan the string letter by letter to count the frequencies of all unique characters in the text, after that, the letters with frequencies greater than 0 and the frequencies are reported side by side. All members...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • Write a program that calculates the frequency of letter occurrences in text. Read ASCII text from...

    Write a program that calculates the frequency of letter occurrences in text. Read ASCII text from standard input. On reaching EOF, print to stdout the normalized frequency of occurrence for each letter a-z that appeared in the input, one per line, in alphabetical order using the format produced by printf( "%c %.4f\n", letter, freq); Letters that occur zero times should not appear in the output. Characters other than lower and upper case letters should be ignored. Lower and upper case...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT