0

#Find out the 15th most frequent word and its frequency. Print both on the screen.

import pprint

abc = open("ABC_Code_of_Conduct.txt", "r")

for item in umb: count_list.append((abc[item],item))

count_list.sort(reverse = True)

print(count_list[0])

print(count_list[14])

Aniket
  • 1
  • 3
  • `abc` is a file object, as the error mentions. What do you mean to do with `abc = open("ABC_Code_of_Conduct.txt", "r")`? If the contents of the files are JSON or a similar format, you need to parse it first. – Carcigenicate Oct 16 '22 at 03:23
  • Shouldn't you be using the `.read()` method to actually read the file as a string? Otherwise you just get the name of the object in memory. – asultan904 Oct 16 '22 at 05:02

1 Answers1

0

Stealing from the answer in Find the nth most common word and count in python:

#Find out the 15th most frequent word and its frequency. Print both on the screen.
import pprint
import collections

count_list = []
abc = "ABC_Code_of_Conduct.txt"
abc_words = open(abc, 'r').read().split()
counter = collections.Counter(abc_words)

nth = 15
print(list(collections.Counter(abc_words).most_common(nth)[-1]))

e.g.:

['respect', 3]