Question

Letter Count : Write a Python script that reads a file and outputs the number of...

Letter Count : Write a Python script that reads a file and outputs the number of words that start with each letter. This means that for every letter we want to count the total number of (nonunique) words that begin with that letter. In your implementation you should ignore the letter case, i.e., consider all words as lower case.

Output the results in the following format (below values are hypothetical): a or A: 120 b or B: 460 c or C: 240 (do this for all letters from a to z in alphabetical order) z or Z: 22

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

If you have any doubts, please give me comment...

Code:

result = {}

try:

fname = input("Enter input filename: ")

fp = open(fname, "r")

for line in fp.readlines():

words = line.strip().split()

for word in words:

word = word.lower()

if word[0] not in result:

result[word[0]] = 0

result[word[0]] += 1

for ch in result:

print(ch+" "+ch.upper()+": "+str(result[ch]))

except FileNotFoundError:

print("Unable to open file")

Add a comment
Know the answer?
Add Answer to:
Letter Count : Write a Python script that reads a file and outputs the number of...
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
  • Hadoop MapReduce program which outputs the number of words that start with each letter. This means...

    Hadoop MapReduce program which outputs the number of words that start with each letter. This means that for every letter we want to count the total number of words that start with that letter. In your implementation ignore the letter case, i.e., consider all words as lower case. You can ignore all non-alphabetic characters.

  • In Python 3, Write a program that reads in a text file that consists of some...

    In Python 3, Write a program that reads in a text file that consists of some standard English text. Your program should count the number of occurrences of each letter of the alphabet, and display each letter with its count, in the order of increasing count. What are the six most frequently used letters?

  • 1. Write a python program that reads a file and prints the letters in increasing order...

    1. Write a python program that reads a file and prints the letters in increasing order of frequency. Your program should convert entire input to lower case and only counts letters a-z. Special characters or spaces should not be counted. Each letter and it's occurrences should be listed on a separate line. 2. Test and verify your program and it's output. ---Please provide a code and a screenshot of the code and output. Thank you. ----

  • Create a python code named LetterCount with a text file named words.txt that contains at least...

    Create a python code named LetterCount with a text file named words.txt that contains at least 100 words in any format - some words on the same line, some alone (this program involves no writing so you should not be erasing this file). Then create a program in the main.py that prompts the user for a file name then iterates through all the letters in words.txt, counting the frequency of each letter, and then printing those numbers at the end...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a program that loads a file called "sample.txt" in read mode, reads its content, and...

    Write a program that loads a file called "sample.txt" in read mode, reads its content, and closes the file. Use exception handling to catch any errors. 2. Compute the letter and punctuation distribution in the file. That is, output the number of a’s, the number of b’s, etc. and the number of commas, dashes, and periods. Ignore case when computing this, so ‘A’ and ‘a’ are the same letter. Use lists. 3.   Compute the number of words in the file....

  • Write a program that reads in a sentence of up to 100 characters and outputs the...

    Write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all strings of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Do not worry about proper names; if their first letters are changed to lowercases, that is acceptable. Treat...

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Create a Python script file called hw.py. Then import the module random: from random import *...

    Create a Python script file called hw.py. Then import the module random: from random import * Thanks in advance! Ex. 1. Write a function called cleanLowerWord that receives a string as a paramcter and retums a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this,...

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
ADVERTISEMENT