Write a python program to calculate and display the frequency of each item in a list using a function CountFrequency() which accepts a list as argument…
its for class 12 student
Write a python program to calculate and display the frequency of each item in a list using a function CountFrequency() which accepts a list as argument…
its for class 12 student
I suppose they will want you to write your own, otherwise you could use collections.Counter.
You can imitate the same idea: Have a dictionary, or perhaps a defaultdict(int). Then, for each element of the list, increment the entry in the dictionary corresponding to that element. At the end, you can divide each value in the dictionary by the total length of the list.
To me, “frequency” just means count.
Define the
CountFrequency()
function that takes a list as input.
Create a dictionary to store the item frequencies.
Iterate over each item in the list.
Before adding check if the item is already a key in the dictionary:
, If it is, increment the corresponding value by 1.
,If it isn’t, add the item as a key with a value of 1.
Iterate over the dictionary to display the items and their frequencies.