-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNLP__.py
More file actions
38 lines (34 loc) · 816 Bytes
/
Copy pathNLP__.py
File metadata and controls
38 lines (34 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from collections import defaultdict
Dic_1={
'a':[10],
'b':[20,30],
'c':[40,10],
'p':[20,30,50]
}
Dic_2={
'p':100,
'q':[40,80],
'a':20,
'b':[80,20],
'c':[10],
'd':[48,78]
}
def merge_dicts(dict1, dict2):
"""
Merges two dictionaries, handling different value types and combining keys.
Args:
dict1: The first dictionary.
dict2: The second dictionary.
Returns:
A new dictionary with merged values.
"""
ans_dict = defaultdict(list)
for key, value in dict1.items():
ans_dict[key].append(value)
for key, value in dict2.items():
ans_dict[key].append(value)
# for key, values in ans_dict.items():
# ans_dict[key] = list(set(values)) # Remove duplicates
# return dict(ans_dict)
ans_dict = merge_dicts(Dic_1, Dic_2)
print(ans_dict)