Sample problem solved by the default dictionary - scenario one

Let's consider a simple problem solved by default dictionary; here we would like to calculate the frequency of elements present in the list. Let's examine the scenario with an example:

from collections import defaultdict

game = defaultdict(int)

list1 = ['cricket', 'badminton', 'hockey' 'rugby', 'golf', 'baseball' , 'football']

for each in list1:
game[each]= game[each]+1

print game

As the default value is initialized with 0 we have incremented it with 1, and, in this way, we can calculate the frequency of elements present in the list:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.135