-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterview05_solution.py
More file actions
22 lines (17 loc) · 968 Bytes
/
Copy pathinterview05_solution.py
File metadata and controls
22 lines (17 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from collections import defaultdict
def mix_colors_numbers(color_number_list):
"""
Return a dictionary of color-numbers pairs from the color_number_list
:param color_list: list of tuples with a color associated to a number
:return: a dictionary of color-numbers pairs sorted by colors and numbers
>>> mix_colors_numbers([('yellow', 5), ('blue', 2), ('yellow', 3), ('blue', 7), ('red', 1)])
{'blue': [2, 7], 'red': [1], 'yellow': [3, 5]}
>>> mix_colors_numbers([('yellow', 1), ('blue', 2), ('yellow', 1), ('blue', 7), ('red', 1)])
{'blue': [2, 7], 'red': [1], 'yellow': [1]}
>>> mix_colors_numbers([('yellow', 1), ('blue', 2), ('blue', 7), ('red', 1), ('blue', 2)])
{'blue': [2, 7], 'red': [1], 'yellow': [1]}
"""
dd = defaultdict(list)
for i in sorted(sorted(color_number_list, key=lambda x: x[1]), key=lambda x: x[0]):
dd[i[0]].append(i[1]) if i[1] not in dd[i[0]] else None
return dict(dd.items())