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 letters and digits and store them in a list
- letters_and_digits = list(temp)
- # count the number of each letter and digit in the file
- letters_and_digits_count = Counter(letters_and_digits)
- # store the number of letters and digits in descending order
- letters_and_digits_count_descending = letters_and_digits_count.most_common()
- # create a dictionary
- d = {}
- # store the list in a dictionary
- d = dict(letters_and_digits_count)
- # print sorted dictionary
- print letters_and_digits_count_descending
Advertisement
Add Comment
Please, Sign In to add comment