Advertisement
Guest User

Untitled

a guest
Aug 30th, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. from collections import Counter, defaultdict
  2.  
  3. def mode(seq):
  4. c = Counter(seq)
  5. x, amount = c.most_common(1)[0]
  6. return [k for k, v in c.items() if v == amount]
  7.  
  8. def separate_by_type(seq):
  9. d = defaultdict(list)
  10. for item in seq:
  11. d[type(item)].append(item)
  12. return d
  13.  
  14. x = [1.0, 2.0, 2.0, 2, 1, 3, 3, 4, 4]
  15. d = separate_by_type(x)
  16. for t, seq in d.items():
  17. print(f"Mode of items with type {t}:")
  18. print(mode(seq))
  19.  
  20. #output:
  21. #Mode of items with type <class 'float'>:
  22. #[2.0]
  23. #Mode of items with type <class 'int'>:
  24. #[3, 4]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement