-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict_flatten.py
More file actions
49 lines (33 loc) · 1.36 KB
/
dict_flatten.py
File metadata and controls
49 lines (33 loc) · 1.36 KB
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
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
def dict_flatten(to_be_flatten_dict, prefix_str = '',joint_str = '_'):
"""
该函数用于将一个多层嵌套的字典转换成一个单层的字典。
temp_dict = {'cascas':'c'
,'ca':{'bgb':{'ujujuj':'vdsx'}}
,'mkm':{'wewrwer':{'bnv':{'qwqs':{'cvbcvb':'xalx'}}}}}
flatten_dict = {}
for item in dict_flatten(temp_dict,joint_str = '_'):
flatten_dict.update(item)
print(flatten_dict)
#将打印
{'cascas': 'c', 'ca_bgb_ujujuj': 'vdsx', 'mkm_wewrwer_bnv_qwqs_cvbcvb': 'xalx'}
"""
for key,value in to_be_flatten_dict.items():
if isinstance(value,dict):
if not prefix_str:
yield from dict_flatten(value,key + joint_str)
else:
yield from dict_flatten(value,prefix_str + key + joint_str)
else:
if not prefix_str:
yield {key:value}
else:
yield {prefix_str + key:value}
if __name__ == '__main__':
temp_dict = {'cascas':'c'
,'ca':{'bgb':{'ujujuj':'vdsx'}}
,'mkm':{'wewrwer':{'bnv':{'qwqs':{'cvbcvb':'xalx'}}}}}
flatten_dict = {}
for item in dict_flatten(temp_dict,joint_str = '_'):
flatten_dict.update(item)
print(flatten_dict)