Hướng dẫn convert nested dictionary to dictionary python - chuyển từ điển lồng nhau sang từ điển python

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'},
]
2

def0____21 def22____23 def4

def0____21 def7def3 def9

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 def62=

Đầ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 def7def3

[
 {'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'},
]
31

flatten_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=3

def00def01

[
 {'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'},
]
83

def00def06

[
 {'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'},
]
83

def00def11def1212

def00def18def19, 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'},
]
83def22def23

'_'0def25

[
 {'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
 {'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
 {'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
03def27

flatten_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'},
]
83

def00def11def1212

'_'0=6=3=2___

def00def11def1212

'_'0=6=3=2___

def def62= 
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} 
 


Làm thế nào để bạn làm phẳng một từ điển lồng nhau trong Python?

Viết trải nghiệm phỏng vấn ..
Python | Chuyển đổi từ điển lồng nhau thành từ điển phẳng ..
Python | Chuyển đổi danh sách từ điển lồng nhau thành gấu trúc DataFrame ..
Tạo một bản dữ liệu gấu trúc từ danh sách các dicts ..
Đọc các tệp CSV trong Python ..
Làm việc với các tệp CSV trong Python ..
Viết các tệp CSV bằng Python ..

Làm cách nào để chuyển đổi một danh sách lồng nhau thành một từ điển?

Chúng ta có thể chuyển đổi một danh sách lồng nhau thành một từ điển bằng cách sử dụng từ điển hiểu.Nó sẽ lặp lại thông qua danh sách.Nó sẽ lấy mục tại INDEX 0 làm khóa và chỉ mục 1 làm giá trị.using dictionary comprehension. It will iterate through the list. It will take the item at index 0 as key and index 1 as value.

Làm thế nào để bạn phá vỡ từ điển lồng nhau trong Python?

Để xóa một mục được lưu trữ trong từ điển lồng nhau, chúng ta có thể sử dụng câu lệnh DEL.Câu lệnh DEL cho phép bạn xóa một đối tượng.Del được viết giống như một câu lệnh Break Python, trên dòng riêng của nó, theo sau là mục trong từ điển mà bạn muốn xóa.use the del statement. The del statement lets you delete an object. del is written like a Python break statement , on its own line, followed by the item in the dictionary that you want to delete.

Làm thế nào để bạn nhận được một giá trị từ một python từ điển lồng nhau?

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).use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).