Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter, defaultdict
- def mode(seq):
- c = Counter(seq)
- x, amount = c.most_common(1)[0]
- return [k for k, v in c.items() if v == amount]
- def separate_by_type(seq):
- d = defaultdict(list)
- for item in seq:
- d[type(item)].append(item)
- return d
- x = [1.0, 2.0, 2.0, 2, 1, 3, 3, 4, 4]
- d = separate_by_type(x)
- for t, seq in d.items():
- print(f"Mode of items with type {t}:")
- print(mode(seq))
- #output:
- #Mode of items with type <class 'float'>:
- #[2.0]
- #Mode of items with type <class 'int'>:
- #[3, 4]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement