Tôi có một danh sách từ điển như thế này
[
{'id':1, 'name': 'name1', 'education':{'university':'university1', 'subject': 'abc1'}},
{'id':2, 'name': 'name2', 'education':{'university':'university2', 'subject': 'abc2'}},
{'id':3, 'name': 'name3', 'education':{'university':'university3', 'subject': 'abc3'}},
]
Và tôi muốn chuyển đổi nó như
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
Có cách nào để giải quyết vấn đề này không.
Các giá trị truy cập bằng cách sử dụng get [] Một cách khác để truy cập [các] giá trị trong từ điển lồng nhau [nhân viên] là sử dụng phương thức Dict.get []. Phương thức này trả về giá trị cho một khóa được chỉ định. Nếu khóa được chỉ định không tồn tại, phương thức get [] sẽ không trả về không [ngăn chặn một keyerror].
Với một từ điển lồng nhau, nhiệm vụ là chuyển đổi từ điển này thành một từ điển được làm phẳng trong đó khóa được phân tách bằng ‘_, trong trường hợp khóa lồng nhau được bắt đầu.
Đưa ra dưới đây là một vài phương pháp để giải quyết nhiệm vụ trên.
Python3
Phương pháp số 1: Sử dụng phương pháp ngây thơ & nbsp;
def
flatten_dict[dd, separator
=
'_'
, prefix
=
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
0 [
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
1[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
2 [
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
3[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
2def
0____21 def
22____23 def
4
def
0____21 def
7def
3 def
9
flatten_dict[dd, separator
9=
=
1=
2=
3=
4=
3=
6=
7=
8=
9
'_'
0=
6=
3=
2=
3=
4=
7'_'
7=
9
'_'
0=
6=
3=
2___
Is
def
def
62=
Đầu ra: & nbsp; init_dictionary {'geek': {'geek': {'for': 7}}, 'geek': {'for': {'geek': 4, 'cho': 1}, ' ': {' Geek ': {' Geek ': 3}}}
initial_dictionary {‘geeks’: {‘Geeks’: {‘for’: 7}}, ‘Geeks’: {‘for’: {‘geeks’: 4, ‘for’: 1}}, ‘for’: {‘geeks’: {‘Geeks’: 3}}}
final_dictionary {‘Geeks_for_for’: 1, ‘geeks_Geeks_for’: 7, ‘for_geeks_Geeks’: 3, ‘Geeks_for_geeks’: 4}
Phương pháp số 2: Sử dụng MutuaBlemapping & NBSP;mutuableMapping
Python3
'']:
4 '']:
5'']:
6 '']:
7
def
'']:
9=
1
2
5____4
7
____21 def
7def
3
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
02[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
04=
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
06[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
2 [
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
08[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
2[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
6 flatten_dict[dd, separator
3[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
18'_'
0
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
20=
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
22[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
8[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
25'_'
0
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
27
____10 flatten_dict[dd, separator
5
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
31flatten_dict[dd, separator
9=
=
1=
2=
3=
4=
3=
6=
7=
8=
9
'_'
0=
6=
3=
2=
3=
4=
7'_'
7=
9
'_'
0=
4=
3=
6=
3=
6=
7, prefix
6, prefix
7=
2=
7=
0=
1
Is
=
2 =
3'']:
0, prefix
7=
6__
Đầu ra: & nbsp; init_dictionary {'geek': {'for': {'for': 1, 'geek': 4}}, 'for': {'geek': {'geek': 3}, 'geeks ': {' Geek ': {' for ': 7}}}
initial_dictionary
{‘Geeks’: {‘for’: {‘for’: 1, ‘geeks’: 4}}, ‘for’: {‘geeks’: {‘Geeks’: 3}}, ‘geeks’: {‘Geeks’: {‘for’: 7}}}
final_dictionary {‘Geeks_for_geeks’: 4, ‘for_geeks_Geeks’: 3, ‘geeks_Geeks_for’: 7, ‘Geeks_for_for’: 1}
Phương pháp số 3: Sử dụng Python Generators & NBSP; & NBSP;Generators
Python3
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
77=
=
1[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
80 =
7, prefix
6[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
85 =
3'_'
0
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
88=
7[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
90[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83'_'
0
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
93=
7'_'
7[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83'_'
0
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
98=
3def
00def
01
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
25=
0[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83def
00def
06
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
25[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
80[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83def
00def
11def
1212
def
00def
18def
19, prefix
6
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83def
22def
23'_'
0def
25
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03def
27flatten_dict[dd, separator
9=
=
1=
2=
3=
4=
3=
6=
7=
8=
9
Is
'_'
0=
4=
3=
6=
3=
6=
7, prefix
6, prefix
7=
2=
7=
0=
1
=
2 =
3'']:
0, prefix
7=
6__
Đầu ra: & nbsp; init_dictionary {'geek': {'for': {'for': 1, 'geek': 4}}, 'for': {'geek': {'geek': 3}, 'geeks ': {' Geek ': {' for ': 7}}}
Phương pháp số 3: Sử dụng Python Generators & NBSP; & NBSP;
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
77=
=
1[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
80 =
7, prefix
6[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
85 =
3
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
8[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
25'_'
0
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
88=
7[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
90[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
83def
00def
11def
1212
'_'
0=
6=
3=
2___
def
00def
11def
1212
'_'
0=
6=
3=
2___
def
def
62=
initial_dictionary {‘for’: {‘geeks’: {‘Geeks’: 3}}, ‘geeks’: {‘Geeks’: {‘for’: 7}}, ‘Geeks’: {‘for’: {‘for’: 1, ‘geeks’: 4}}}
final_dictionary {‘Geeks_for_geeks’: 4, ‘for_geeks_Geeks’: 3, ‘Geeks_for_for’: 1, ‘geeks_Geeks_for’: 7}