4.6k views
3 votes
Take a list of numbers that range in value from 0 to 9 and output how many occurrences of each number exist.

User LightBox
by
7.7k points

1 Answer

0 votes

Answer:

The program in Python is as follows:

def count_occur(myList):

freq = {}

for item in myList:

if item in freq:

freq[item] += 1

else:

freq[item] = 1

for item in freq:

print(item," ",freq[item])

Step-by-step explanation:

This gets the list from the main

def count_occur(myList):

This initializes an empty dictionary

freq = {}

This iterates through the list and count the occurrence of each item

for item in myList:

if item in freq:

freq[item] += 1

else:

freq[item] = 1

This iterates through the dictionary

for item in freq:

This prints each dictionary item and the frequency separated by -

print(item," ",freq[item])

User Shankar Regmi
by
7.6k points