Advertisement
mouradsme

Untitled

Mar 26th, 2022
1,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. def freq(l):
  2.     result = {}
  3.     # Prepare the dictionnary of frequencies
  4.     for a in l:
  5.         result[a] = result[a]+ 1 if a in result else 1
  6.     # Get the max value -frequency- in the dict.
  7.     m = max(result.values())
  8.     # Find all the elements that have the max frequency m
  9.     keys = [k for k, v in result.items() if v == m]
  10.     tup = []
  11.     for i in keys:
  12.         tup.append((i, result[i]))
  13.     return tup
  14.  
  15. # Example:
  16. print(freq([1, 1, 3, 3, 3, 4, 5, 4, 5, 5]))
  17.  
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement