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
23d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2 y=sorted[d.values[]]
print y
25OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]8
y=sorted[d.values[]]
print y
27y=sorted[d.values[]]
print y
28d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2 y=sorted[d.values[]]
print y
30Input:
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
2OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8y=sorted[d.values[]]
print y
33
y=sorted[d.values[]]
print y
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
y=sorted[d.values[]]
print y
y=sorted[d.values[]]
print y
y=sorted[d.values[]]
print y
34d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2 y=sorted[d.values[]]
print y
36Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 64
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 62
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41} x=sorted[d.keys[]] print x
OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]
8
41______iterkeys[] function.y=sorted[d.values[]] print y
- 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:
- Đầ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 [].
- 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ó.
- 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]]
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
1d={"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
3d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5y=sorted[d.values[]]
print y
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5y=sorted[d.values[]]
print y
8d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2 z=sorted[d.items[]]
print z
1d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5z=sorted[d.items[]]
print z
4d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"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]]
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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]]
3d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"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]]
6d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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]]
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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 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 64
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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 68
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 69
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 61
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 62
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 63
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 64
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 65
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 66
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 61
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 68
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2Input: 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 64
[[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
0Input: 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
0OrderedDict[[['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 66
[[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]]
21Is
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 61
[[1, 89], [2, 3], [3, 0], [4, 5]]
30Output:
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]]
0Python3
[[1, 89], [2, 3], [3, 0], [4, 5]]
8 [[1, 89], [2, 3], [3, 0], [4, 5]]
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
1d={"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
3d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5y=sorted[d.values[]]
print y
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5y=sorted[d.values[]]
print y
8d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2 z=sorted[d.items[]]
print z
1d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5z=sorted[d.items[]]
print z
4d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"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]]
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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]]
3d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"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]]
6d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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]]
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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 68
[[1, 89], [2, 3], [3, 0], [4, 5]]
77d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
[[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]]
84Input: 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 64
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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 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 64
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 66
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 61
[[1, 89], [2, 3], [3, 0], [4, 5]]
94d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2Input: 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 64
[[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
0Input: 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
0OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]3
Output:
[[1, 89], [2, 3], [3, 0], [4, 5]]
1Ví 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]]
2Python3
[[1, 89], [2, 3], [3, 0], [4, 5]]
8 [[1, 89], [2, 3], [3, 0], [4, 5]]
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
1d={"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
3d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
6d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5y=sorted[d.values[]]
print y
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5y=sorted[d.values[]]
print y
8d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2 z=sorted[d.items[]]
print z
1d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
5z=sorted[d.items[]]
print z
4d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
2d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"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]]
0d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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]]
3d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0d={"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]]
6d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
7d={"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]]
9d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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 68
[[1, 89], [2, 3], [3, 0], [4, 5]]
77d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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]]
83d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
63Input: 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 64
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Input: 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 61
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 62
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 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 64
d={"John":36,"Lucy":24,"Albert":32,"Peter":18,"Bill":41}
x=sorted[d.keys[]]
print x
0Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 61
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 62
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 63
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 64
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 65
[[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
0Input: 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
0OrderedDict[[['rajnish', '9'], ['ravi', '10'], ['sanjeev', '15'], ['suraj', '32'], ['yash', '2']]]3
Output:
[[1, 89], [2, 3], [3, 0], [4, 5]]
3Ví 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]]
4Python3
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 66
[[1, 89], [2, 3], [3, 0], [4, 5]]
10[[1, 89], [2, 3], [3, 0], [4, 5]]
02y=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]]
02d={"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]]
02y=sorted[d.values[]]
print y
17[[1, 89], [2, 3], [3, 0], [4, 5]]
21Input: 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 61
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 62
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 64
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
0Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 61
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 62
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 63
Task 1:- key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 1 2 3 4 5 64
[[1, 89], [2, 3], [3, 0], [4, 5]]
91Input: 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 61
y=sorted[d.values[]]
print y
50
Output:
[[1, 89], [2, 3], [3, 0], [4, 5]]
5