Page 396 - Computer Science V2.0 Class 11
P. 396
13.6.2 Quick Programming Question
Write a function to count the frequency of each element in a list. Make use of it to write a program that accepts a list
of integers and displays the frequency of each element in the list.
Program 13.2: Count the frequency of each element of a list
01 def frequency(myList):
02 '''
03 Objective: To compute frequency of each element in myList
04 Input Parameters:
05 myList: list
06 Return Value: None
07 '''
08
09 uniqueMyList = set(myList)
10 for item in uniqueMyList:
11 print(f'Count of {item} in list is: {myList.count(item)}')
12
13 #main program segment
14 '''
15 Objective: To count the frequency of each element in mylist
16 User Interface:
17 User is asked to enter:
18 number of elements in the list
19 list elements one by one
20 '''
21 myList = []
22 numElements = int(input('Enter size of the list: '))
23 print('Enter each element and press enter: ')
24 for i in range(0, numElements): #list input element-wise
25 num = int(input())
26 myList.append(num)
27 print('Contents of list:', myList)
28 frequency(myList)
29
Sample Output:
>>> Enter size of the list: 7
>>> Enter each element and press enter:
3
4
3
2
3
2
4
Contents of list: [3, 4, 3, 2, 3, 2, 4]
Count of 2 in list is: 2
Count of 3 in list is: 3
Count of 4 in list is: 2
13.6.3 Updating Marks in an Examination
Program 13.3 Write a program that manages the results of students in an examination. It performs the following tasks:
1. Accepts the number of students from the user.
2. Accepts the result of each student from the user in the form of a list [rollNo, marks].
3. Constructs the list result comprising the list [rollNo, marks] from step 2, for each student.
382 Touchpad Computer Science (Ver. 2.0)-XI

