Hướng dẫn check length dictionary python - kiểm tra chiều dài từ điển python

Để tính độ dài của từ điển, chúng ta có thể sử dụng phương pháp Len [] tích hợp Python. Phương thức Len [] & nbsp; trả về số lượng khóa trong từ điển Python.

Python dict Len []

Cú pháp: Len [Dict]len[Dict]

Return: nó trả về một số nguyên là độ dài của chuỗi. & Nbsp;It returns an integer which is the length of the string. 

Name:Steve
Age:30
Designation:Programmer

Ví dụ cơ bản về việc tìm kiếm độ dài của từ điển

Python3

dict1

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
4
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
8
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
5
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
2
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
6
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
7

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
5
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
3

Output:

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3

Tìm độ dài của từ điển lồng nhau

Xem xét các chi tiết sau đây về một người:

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India

Vấn đề khi cố gắng tìm độ dài của từ điển lồng nhau:

Python3

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
4
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
6

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
2
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
9
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
4
The length of the nested dictionary is: 7
1

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
6
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
9
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
8
The length of the nested dictionary is: 7
1

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
9
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
2
The length of the nested dictionary is: 7
1

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
6
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
9
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
8
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
1
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
9
The length of the dictionary is  9
3
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
6
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
9
The length of the dictionary is  9
8

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:
          St_number:4
          St_name:Brigade  Road
      City:Bangalore
      Country:India
4
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
5
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:
          St_number:4
          St_name:Brigade  Road
      City:Bangalore
      Country:India
7

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Nested dictionary length: 11
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
5
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Nested dictionary length: 11
3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Nested dictionary length: 11
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
5
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Nested dictionary length: 11
9

Output:

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4

Ở đây, bất kỳ phương pháp nào bạn áp dụng, bạn sẽ chỉ nhận được ‘4 là đầu ra. Nhưng số lượng mục nhập thực tế là ‘7. Các chìa khóa là tên, tuổi, chỉ định, địa chỉ, đường phố, thành phố và quốc gia. Phương pháp xem xét từ điển bên ngoài là giá trị cho một trong các khóa là một giá trị duy nhất.

Để khắc phục vấn đề này, chúng ta cần thêm rõ ràng độ dài của từ điển bên trong vào cái bên ngoài. Nó có thể được mã hóa như được đưa ra dưới đây: & nbsp;

Python3

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
4
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0dict1 2

dict1 3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
4
The length of the nested dictionary is: 7
1

dict1 3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
8
The length of the nested dictionary is: 7
1

dict1 3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
2
The length of the nested dictionary is: 7
1

dict1 3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
11
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
8
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
1
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
The length of the dictionary is  9
3
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
The length of the dictionary is  9
8

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
11
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
29
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
31
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
34
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
35
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
37
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
39

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
42
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
43

Output:

The length of the nested dictionary is: 7

Bây giờ nó hoạt động tốt !!! Tuy nhiên, có thể lập trình rõ ràng để thêm độ dài của từ điển bên trong mỗi lần không? Điều gì sẽ xảy ra nếu chúng ta không biết trước đây có bao nhiêu từ điển bên trong? Bây giờ hãy xem xét các chi tiết sau:However, is it possible to explicitly program to add the length of inner dictionaries every time? What if we do not know in prior how many inner dictionaries are there? Now consider the following detail:

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India

Ở đây chúng tôi có hai từ điển bên trong. Nó không phải là một cách tối ưu hóa để thêm độ dài của từ điển bên trong mỗi lần. Chúng ta có thể giải quyết vấn đề này bằng cách kết hợp isInstance [] với phương thức len []. Ý tưởng là trước tiên để lưu trữ chiều dài của toàn bộ từ điển trong một biến [giả sử ‘chiều dài ở đây]. Sau đó lặp lại thông qua tất cả các giá trị [] của từ điển và kiểm tra xem đó có phải là một ví dụ của dict hay không. Nếu ‘đúng thì độ dài của từ điển bên trong đó được tìm thấy và thêm vào độ dài thay đổi. Theo cách này, tổng chiều dài của một từ điển lồng nhau có thể được tìm thấy.

Ví dụ 1: Tìm độ dài của từ điển lồng nhau sử dụng linh hoạt cho vòng lặp:

Khi chúng ta có nhiều khóa hơn trong một từ điển có giá trị một lần nữa là từ điển. Sau đó, chúng ta cần kiểm tra loại giá trị của mỗi khóa, nếu nó là một từ điển, thì chúng ta sử dụng Len [] trên giá trị và thêm giá trị vào độ dài của từ điển bên ngoài.

Python3

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
4
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

dict1 3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
52
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
53
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
4
The length of the nested dictionary is: 7
1

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
52
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
58
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
60

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
62

dict1 3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
8
The length of the nested dictionary is: 7
1

dict1 3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
2
The length of the nested dictionary is: 7
1

dict1 3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
81
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
1
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
The length of the dictionary is  9
3
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
The length of the dictionary is  9
8

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
93

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
29
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
31
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
34

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
00
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
01
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
02
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
03

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
05
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
06
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
07
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
08
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
09

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
10
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
31
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
35
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
15

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
18
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
43

Output:

The length of the dictionary is  9

Lưu ý: Cách tiếp cận này sẽ chỉ hoạt động khi việc làm tổ của các từ điển chỉ lên tới 2 cấp độ.This approach will only work when the nesting of the dictionaries is only upto 2 levels.

Nếu từ điển được lồng sâu hơn như dưới đây:

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:
          St_number:4
          St_name:Brigade  Road
      City:Bangalore
      Country:India

Ví dụ 2: Sử dụng đệ quy để tìm độ dài của từ điển lồng nhau:

Ở đây chúng tôi đã sử dụng một hàm đệ quy Count_nested_len [] để đếm độ dài của mỗi từ điển, chúng tôi lặp lại các khóa của từ điển, ngay khi một giá trị là từ điển, chúng tôi nhận lại từ từ điển đó.

Python3

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
4
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

dict1 3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
2
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
52
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
53
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
4
The length of the nested dictionary is: 7
1

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
52
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
58
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
60

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
62

dict1 3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
8
The length of the nested dictionary is: 7
1

dict1 3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
0
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
2
The length of the nested dictionary is: 7
1

dict1 3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
52
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
1

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
59
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
60
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
62
The length of the nested dictionary is: 7
1

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
59
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
65
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
81

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
52
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
62

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
1
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
The length of the dictionary is  9
3
The length of the nested dictionary is: 7
1

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
The length of the dictionary is  9
6
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
3
The length of the dictionary is  9
8

Name:
    first name:Steve
    last name:Jobs
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
93

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
29
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
3

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
83
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
84

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
31
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
2
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
89

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
00
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
92
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
02
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
94

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
10
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
05
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
06
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
98
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
08
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
09

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
01
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
31
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
35
Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
0
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
05

len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
7
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
07
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
08

Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
4
Name:Steve
Age:30
Designation:Programmer
address:
      Street:Brigade  Road
      City:Bangalore
      Country:India
5
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
11
The length of the nested dictionary is: 7
1

Dictionary: {'Name': 'Steve', 'Age': 30, 'Designation': 'Programmer'}
Length of dictionary: 3
29
len[] method : 4
len[] method with keys[] : 4
len[] method with values[]: 4
14

Output:

Nested dictionary length: 11

Chủ Đề