Sunday, 16 September 2018

Prog-25 python programming counting the frequency of elements in a list using a dictionary

#25A count the number of occurrences(frequency) without using count function

def countX(lst,x): count = 0 for ele in lst: if (ele == x): count = count + 1 return count # Driver Code lst = [8, 6, 8, 10, 8, 20, 10, 8, 8] x = 8 print(countX(lst, x))

Ans:5




#25B count the number of occurrences(frequency) without using count function

def countX(lst, x):
    return lst.count(x)
  
# Driver Code
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print(countX(lst, x))

Ans: 5



# 25C python programming counting the frequency of elements in a list using a dictionary
a = [1,8,1,2,2,9,2,3,1,4,5,5] #list d = {x:a.count(x) for x in a} #dictionary print(d)

Ans.
{1: 3, 8: 1, 2: 3, 9: 1, 3: 1, 4: 1, 5: 2}


No comments:

Post a Comment