Question

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 that there is no punctuation and that all of the words are lower case.

Hard-code the name of the input file as input.asc.

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

#input.asc

write a python program to read lines of text from a file
for each word 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

#source.py

f=open("input.asc","r")
temp=[]
for line in f.readlines():
   sublist=line.split(" ")
   sublist[len(sublist)-1]=sublist[len(sublist)-1].replace("\n","")
   temp.append(sublist)
list=[]
for i in range(len(temp)):
   for j in range(len(temp[i])):
       list.append(temp[i][j])
sub_unique=set(list)
data=[]
for i in sub_unique:
   a=list.count(i)
   data.append([i,a])
print(data) #cout the data
value=sorted(data,key = lambda i: i[1],reverse=True)
for i in range(20):
   print(value[i]) #top 20 values based on count
print(sorted(data)) #alphabetical order
tempc=sorted(data)
print("\n")
for i in range(len(tempc)):
   tempc[i][0],tempc[i][1]=tempc[i][1],tempc[i][0]
print(tempc)
  
  
#output:

[for, 2] [characters, 2] E, 1] [times, 1] [twenty, 1] [on, 1] [[, 1], [a, 3], [alphabetical, 1], [and, 2],

#if you have any doubts comment below...

Add a comment
Know the answer?
Add Answer to:
Write a Python program to read lines of text from a file. For each word (i.e,...
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
  • 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...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • (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...

  • Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

    Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words). Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers: The count of lines/sentences The count...

  • (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...

  • Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1...

    Python: The attached text file: https://drive.google.com/open?id=1Hcj4Ey4NbKHn5-vyF-foR3iny0aX8y1c Unique Words Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and a list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not found’ error. Store each word as an element of list. Be sure not...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Write a program that inputs a text file. The program should print the unique words in...

    Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Note: The sort function sorts first numbers, then capital letters, then lowercase letters. That is fine for this assignment as you can see from the sample output. But even if the same word appears multiple times in the file it should only be displayed once in the output. (python)

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • Need some help on this Python Problem called "unique words" the text.txt file can be any...

    Need some help on this Python Problem called "unique words" the text.txt file can be any .txt file with words in it. Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not...

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