Question

PYTHON Make a dictionary with a list inside of it to keep track of word frequency...

PYTHON

Make a dictionary with a list inside of it to keep track of word frequency in a text file. example {1:['and','there','we'], 2: ['she','he']}

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

CODE:

#opening the file
file = open("C:/Users/Win10/Documents/input.txt","r")
#creating a list lines which will contain all the words of the file
lines = []
#dictionary frequency which will the frequency of each word in the file
frequency = {}
#reading the lines in the file
lines = file.readlines()
#splitting the line read into words
lines = lines[0].split()
#iterating through all the words read from the file
for i in lines:
    a = 0
    for j in lines:
        #finding the frequency of each word
        if i == j:
            a += 1
    #adding the word as key and its frequency in a dictionary
    frequency[i] = a
    #removing the word from the list whose frequency was counted just now so that it doesnt gets repeated in the dictionary
    lines.remove(i)

#creating a dictionaty that will hold the word frequency
f = {}
#creating an empty list with the various frequencies as the key
for i in frequency.items():
    f[i[1]] = []
#adding the word of a given frequency to its respective key in the dictionary f
for i in frequency.items():
    f[i[1]].append(i[0])
#printing the key and its list in each line
for i in f:
    print(i," ",f[i])
#printing the dictionary
print(f)

_________________________________________

Refer to code images for indentation:

___________________________________________________

OUTPUT:

C:\Users\Win10\PycharmProjects\hw4\venv\Scripts\python.exe C:\Users\Win10\PycharmProjects\hw4\venv\script.py
1 ['There', 'once', 'young', 'Filippo', 'renowned', 'all', 'men', 'Tuscany', 'As', 'happens', 'with', 'lady', 'Monna', 'day', 'charming', 'that', 'there', 'order', 'win', 'participated', 'jousts', 'tournaments', 'feasts', 'spent', 'money', 'restraint', 'less', 'than', 'cared', 'things', 'on', 'behalf', 'did', 'care', 'him', 'Now', 'spending', 'beyond', 'means', 'lost', 'wealth', 'became', 'name', 'whose', 'very', 'falcon', 'world']
2 ['Florence', 'named', 'the', 'one', 'beautiful', 'love', 'she', 'Federigo', 'nothing', 'as', 'but', 'little']
3 ['of', 'most', 'he', 'to']
6 ['his', 'in', 'and']
4 ['for', 'was']
{1: ['There', 'once', 'young', 'Filippo', 'renowned', 'all', 'men', 'Tuscany', 'As', 'happens', 'with', 'lady', 'Monna', 'day', 'charming', 'that', 'there', 'order', 'win', 'participated', 'jousts', 'tournaments', 'feasts', 'spent', 'money', 'restraint', 'less', 'than', 'cared', 'things', 'on', 'behalf', 'did', 'care', 'him', 'Now', 'spending', 'beyond', 'means', 'lost', 'wealth', 'became', 'name', 'whose', 'very', 'falcon', 'world'], 2: ['Florence', 'named', 'the', 'one', 'beautiful', 'love', 'she', 'Federigo', 'nothing', 'as', 'but', 'little'], 3: ['of', 'most', 'he', 'to'], 6: ['his', 'in', 'and'], 4: ['for', 'was']}

Process finished with exit code 0
_____________________________________________

INPUT FILE:

input.txt

There was once in Florence a young man named Federigo the son of Messer Filippo Alberighi renowned above all other men in Tuscany for his prowess in arms and for his courtliness As often happens to most gentlemen he fell in love with a lady named Monna Giovanna in her day considered to be one of the most beautiful and one of the most charming women that ever there was in Florence and in order to win her love he participated in jousts and tournaments organized and gave feasts and spent his money without restraint but she no less virtuous than beautiful cared little for these things done on her behalf nor did she care for him who did them Now as Federigo was spending far beyond his means and was taking nothing in as easily happens he lost his wealth and became poor with nothing but his little farm to his name from whose revenues he lived very meagerly and one falcon which was among the best in the world

_________________________________________________________________

Feel free to ask any questions in the comments section if required
Thank You!

Add a comment
Know the answer?
Add Answer to:
PYTHON Make a dictionary with a list inside of it to keep track of word frequency...
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...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • Python Programming Write a program that counts how often a word occurs in a text file....

    Python Programming Write a program that counts how often a word occurs in a text file. Input: Ask the user for the name of an ASCII text file. Output: Display the numbers of top frequently appeared words and their frequencies. Pseudocode: 1) Read the file 2) Split the file into words 3) Count each word 4) Sort the words by frequencies, starting with the most frequent ones 5) Internally you should use some sort of data structure to keep track...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

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

  • Solve it in Python 4. An alternade is a word in which its letters, taken alternatively...

    Solve it in Python 4. An alternade is a word in which its letters, taken alternatively in a strict sequence, and used in the same order as the original word, make up at least two other words. All letters must be used, but the smaller words are not necessarily of the same length. For example, a word with seven letters where every second letter is used will produce a four-letter word and a three-letter word. Here are two examples: "board":...

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

  • For Python [25 pts] Write the method divisorList(aList, divisor) that takes in a list with elements...

    For Python [25 pts] Write the method divisorList(aList, divisor) that takes in a list with elements of any data type and a divisor which is in the form of an integer. The output is a sorted list of only the integers that are divisible by the divisor. Example: divisorList([1, 1, 12, 'a', 90, 34], 2) will return [12, 34, 90] since 12, 90, and 34 are all divisible by two. Note that the returned list is sorted from smallest to...

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

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

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