Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program that reads data from a text file
- # and counts the frequency of words in the file
- # and displays the frequency in descending order
- from collections import Counter
- # open file
- file = open("sample2CAP.txt", "r")
- # store the file contents in the temp variable
- temp = file.read()
- # break data into separate words and store them in a list
- words = list(temp.split())
- # count the number of each word in the file
- words_count = Counter(words)
- # store the number of words in descending order
- words_count_descending = words_count.most_common()
- # create a dictionary
- d = {}
- # store the list in a dictionary
- d = dict(words_count)
- # print sorted dictionary
- print words_count_descending
Advertisement
Add Comment
Please, Sign In to add comment