Computer Science, asked by Anonymous, 1 month ago

URGENT!!!!
Write a program that takes a list of words and creates a dictionary with frequency (number of occurences) of word as key and list of words for that frequency as value.

For example, if list is given as [‘the’,’of’, ‘an’, ‘is’, ‘an’, ‘the’]
Then dictionary should be {2:[‘the’,’an’],1:[‘of’,’is’]}

Answers

Answered by anindyaadhikari13
6

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Python.

list=list()

n=int(input('How many elements for the list? '))

print('Enter the items...')

for i in range(n):

   list.append(input('Enter: '))

set=set(list)

dict=dict()

for i in range(n):

   dict[i]=[x for x in set if list‎.‎count(x)==i]

   if not dict[i]:

       del(dict[i])

print(dict)

\textsf{\large{\underline{Sample I/O}:}}

#1

How many elements for the list? 6

Enter the items...

Enter: the

Enter: of

Enter: an

Enter: is

Enter: an

Enter: the

{1: ['is', 'of'], 2: ['the', 'an']}

—————————————————————————————

#2

How many elements for the list? 5

Enter the items...

Enter: alpha

Enter: beta

Enter: delta

Enter: gamma

Enter: alpha

{1: ['delta', 'gamma', 'beta'], 2: ['alpha']}

Attachments:
Similar questions