Hướng dẫn python sort dictionary by key - từ điển sắp xếp python theo khóa

Có một cách dễ dàng để sắp xếp một từ điển.

Theo câu hỏi của bạn,

Giải pháp là :

c={2:3, 1:89, 4:5, 3:0}
y=sorted[c.items[]]
print y

[Trong đó C, là tên của từ điển của bạn.]

Chương trình này cung cấp đầu ra sau:

[[1, 89], [2, 3], [3, 0], [4, 5]]

như bạn muốn.

Một ví dụ khác là:

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x

Cung cấp đầu ra: ________ 16

y=sorted[d.values[]]
print y

Cung cấp đầu ra: ________ 17

z=sorted[d.items[]]
print z

Cung cấp đầu ra:

[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]

Do đó, bằng cách thay đổi nó thành các khóa, giá trị và vật phẩm, bạn có thể in như những gì bạn muốn. Điều này sẽ giúp!

y=sorted[d.values[]]
print y
23
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
y=sorted[d.values[]]
print y
25
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8
y=sorted[d.values[]]
print y
27

y=sorted[d.values[]]
print y
28
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
y=sorted[d.values[]]
print y
30
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8
y=sorted[d.values[]]
print y
33

y=sorted[d.values[]]
print y
34
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
y=sorted[d.values[]]
print y
36
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
4
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2

  • d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
    x=sorted[d.keys[]]
    print x
    
    0
    OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
    8
    y=sorted[d.values[]]
    print y
    
    41______iterkeys[] function.
  • Trong bài viết này, chúng tôi sẽ thảo luận về cách chúng tôi sắp xếp một từ điển theo giá trị và khóa trong Python.
  • Cần sắp xếp trong từ điểniteritems[], key = lambda [k, v] : [v, k]]

Chúng tôi cần sắp xếp dữ liệu để giảm độ phức tạp của dữ liệu và làm cho các truy vấn nhanh hơn và hiệu quả hơn. Do đó, việc phân loại là rất quan trọng khi chúng ta đang xử lý một lượng lớn dữ liệu. Ở đây, chúng tôi sẽ sử dụng phương pháp sau:

  1. Đầu tiên, sắp xếp các khóa theo thứ tự abc bằng cách sử dụng chức năng key_value.iterkeys [].
  2. Thứ hai, sắp xếp các phím theo thứ tự bằng cách sử dụng hàm được sắp xếp [key_value] và in giá trị tương ứng với nó.
  3. Thứ ba, sắp xếp các giá trị theo bảng chữ cái bằng cách sử dụng key_value.iteritems [], key = lambda [k, v]: [v, k]]

Dưới đây là các nhiệm vụ chính cần thiết để được thực hiện sắp xếp từ điển theo giá trị và các khóa trong Python. Displaying the Keys in sorted order

Tạo một từ điển và hiển thị các phím danh sách của nó theo thứ tự bảng chữ cái.

Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 

Python3

[[1, 89], [2, 3], [3, 0], [4, 5]]
8
[[1, 89], [2, 3], [3, 0], [4, 5]]
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
1
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
y=sorted[d.values[]]
print y
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
y=sorted[d.values[]]
print y
8
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
z=sorted[d.items[]]
print z
1

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
z=sorted[d.items[]]
print z
4
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
6
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
3
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
8
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
1
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
2
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
3
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
4
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
5

Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
6
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
8
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

[[1, 89], [2, 3], [3, 0], [4, 5]]
8
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
5

Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
6
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
222222
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
0
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
1

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
3

Output:

Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 

Ví dụ 2: Sắp xếp từ điển theo khóa & nbsp; 

Trong ví dụ này, chúng tôi sẽ sắp xếp theo thứ tự từ vựng & nbsp; lấy loại khóa Key làm chuỗi.

Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]

Python3

OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
4
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
5
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
6
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
7

OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[[1, 89], [2, 3], [3, 0], [4, 5]]
00
[[1, 89], [2, 3], [3, 0], [4, 5]]
01
[[1, 89], [2, 3], [3, 0], [4, 5]]
022.

Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
6
[[1, 89], [2, 3], [3, 0], [4, 5]]
10
[[1, 89], [2, 3], [3, 0], [4, 5]]
02
[[1, 89], [2, 3], [3, 0], [4, 5]]
12
[[1, 89], [2, 3], [3, 0], [4, 5]]
04
[[1, 89], [2, 3], [3, 0], [4, 5]]
14
[[1, 89], [2, 3], [3, 0], [4, 5]]
02
[[1, 89], [2, 3], [3, 0], [4, 5]]
16
[[1, 89], [2, 3], [3, 0], [4, 5]]
04
[[1, 89], [2, 3], [3, 0], [4, 5]]
18
[[1, 89], [2, 3], [3, 0], [4, 5]]
02
[[1, 89], [2, 3], [3, 0], [4, 5]]
20
[[1, 89], [2, 3], [3, 0], [4, 5]]
21

Is

Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
[[1, 89], [2, 3], [3, 0], [4, 5]]
30

Output:

OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]

Ví dụ 3: Sắp xếp các khóa và giá trị theo thứ tự bảng chữ cái bằng cách sử dụng khóaSorting the Keys and Values in Alphabetical Order using the Key

Trong ví dụ này, chúng tôi đang cố gắng sắp xếp từ điển theo các khóa và giá trị trong Python. Ở đây chúng tôi đang sử dụng một trình lặp qua giá trị từ điển để sắp xếp các khóa.

[[1, 89], [2, 3], [3, 0], [4, 5]]
0

Python3

[[1, 89], [2, 3], [3, 0], [4, 5]]
8
[[1, 89], [2, 3], [3, 0], [4, 5]]
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
1
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
y=sorted[d.values[]]
print y
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
y=sorted[d.values[]]
print y
8
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
z=sorted[d.items[]]
print z
1

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
z=sorted[d.items[]]
print z
4
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
6
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
8
[[1, 89], [2, 3], [3, 0], [4, 5]]
77

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
[[1, 89], [2, 3], [3, 0], [4, 5]]
81
[[1, 89], [2, 3], [3, 0], [4, 5]]
08

[[1, 89], [2, 3], [3, 0], [4, 5]]
83
[[1, 89], [2, 3], [3, 0], [4, 5]]
84
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
3
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
6
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
[[1, 89], [2, 3], [3, 0], [4, 5]]
94
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

[[1, 89], [2, 3], [3, 0], [4, 5]]
8
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
5

Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
6
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
222222
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
0
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
1

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
3

Output:

[[1, 89], [2, 3], [3, 0], [4, 5]]
1

Ví dụ 2: Sắp xếp từ điển theo khóa & nbsp;Sorting the Keys and Values in alphabetical using the value

Trong ví dụ này, chúng tôi sẽ sắp xếp theo thứ tự từ vựng & nbsp; lấy loại khóa Key làm chuỗi.

[[1, 89], [2, 3], [3, 0], [4, 5]]
2

Python3

[[1, 89], [2, 3], [3, 0], [4, 5]]
8
[[1, 89], [2, 3], [3, 0], [4, 5]]
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
1
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
y=sorted[d.values[]]
print y
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
y=sorted[d.values[]]
print y
8
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
z=sorted[d.items[]]
print z
1

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
z=sorted[d.items[]]
print z
4
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
6
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[['Albert', 32], ['Bill', 41], ['John', 36], ['Lucy', 24], ['Peter', 18]]
9

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
8
[[1, 89], [2, 3], [3, 0], [4, 5]]
77

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
60
[[1, 89], [2, 3], [3, 0], [4, 5]]
08

[[1, 89], [2, 3], [3, 0], [4, 5]]
83
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
63
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
3
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
1
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
2
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
3
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
4
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
5

[[1, 89], [2, 3], [3, 0], [4, 5]]
8
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
3

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
5

Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
6
Input:
key_value['ravi'] = '10'       
key_value['rajnish'] = '9'
key_value['sanjeev'] = '15'
key_value['yash'] = '2'
key_value'suraj'] = '32'

Output:
[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]
7
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
222222
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
0
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
1

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
3

Output:

[[1, 89], [2, 3], [3, 0], [4, 5]]
3

Ví dụ 2: Sắp xếp từ điển theo khóa & nbsp;

Trong ví dụ này, chúng tôi sẽ sắp xếp theo thứ tự từ vựng & nbsp; lấy loại khóa Key làm chuỗi.

[[1, 89], [2, 3], [3, 0], [4, 5]]
4

Python3

OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
4
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
5
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
6
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
7

OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2
[[1, 89], [2, 3], [3, 0], [4, 5]]
00
[[1, 89], [2, 3], [3, 0], [4, 5]]
01
[[1, 89], [2, 3], [3, 0], [4, 5]]
022.

Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
6
[[1, 89], [2, 3], [3, 0], [4, 5]]
10
[[1, 89], [2, 3], [3, 0], [4, 5]]
02
y=sorted[d.values[]]
print y
09
[[1, 89], [2, 3], [3, 0], [4, 5]]
04
[[1, 89], [2, 3], [3, 0], [4, 5]]
14
[[1, 89], [2, 3], [3, 0], [4, 5]]
02
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6
[[1, 89], [2, 3], [3, 0], [4, 5]]
04
[[1, 89], [2, 3], [3, 0], [4, 5]]
18
[[1, 89], [2, 3], [3, 0], [4, 5]]
02
y=sorted[d.values[]]
print y
17
[[1, 89], [2, 3], [3, 0], [4, 5]]
21

Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
2
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8
Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
4

Is

Ví dụ 3: Sắp xếp các khóa và giá trị theo thứ tự bảng chữ cái bằng cách sử dụng khóa

Trong ví dụ này, chúng tôi đang cố gắng sắp xếp từ điển theo các khóa và giá trị trong Python. Ở đây chúng tôi đang sử dụng một trình lặp qua giá trị từ điển để sắp xếp các khóa.

d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
1
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
2
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
3
Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}

1 2 3 4 5 6 
4
[[1, 89], [2, 3], [3, 0], [4, 5]]
91

Input:
key_value[2] = '56'       
key_value[1] = '2'
key_value[4] = '12'
key_value[5] = '24'
key_value[6] = '18'
key_value[3] = '323'

Output:
1 2 3 4 5 6 
1
y=sorted[d.values[]]
print y
50

Output:

[[1, 89], [2, 3], [3, 0], [4, 5]]
5

Bài Viết Liên Quan

Chủ Đề