Chỉ cần tạo một gencomp của giá trị tuple, khóa và sắp xếp nó. Các mặt hàng in
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
x = sorted[[[v,k] for k,v in d.items[]]]
print[x[-2][1]]
print[x[-3][1]]
result:
b
d
[sẽ thất bại nếu Dict không có ít nhất 3 mục]
Hoặc trực tiếp với tham số key
[tránh sắp xếp lại dữ liệu]
x = sorted[d.items[],key=[lambda i: i[1]]]
print[x[-2][0]]
print[x[-3][0]]
result:
b
d
BTW Tránh sử dụng dict
làm biến.
Chỉnh sửa: Vì có một số giá trị giống hệt nhau, bạn có thể muốn nhận được các giá trị tốt nhất 2 giây và các chữ cái được liên kết. Bạn phải làm điều đó khác nhau. Tôi sẽ tạo một danh sách mặc định bằng cách sử dụng khóa làm giá trị và lưu trữ trong danh sách, sau đó sắp xếp nó như được thực hiện trong mã trên:
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
result:
[4, ['b']]
[3, ['c', 'd']]
Phương pháp số 2: Sử dụng The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
8 Nhiệm vụ này có thể được thực hiện bằng hàm The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
9. Đây là chức năng sẵn có trong thư viện The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are ['for', 'best', 'is']
0, trong nội bộ thực hiện nhiệm vụ này và có thể được sử dụng để thực hiện nó bên ngoài. Có nhược điểm của việc in chỉ các phím không giá trị.
3 b
d
The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are ['for', 'best', 'is']
2
5 b
d
The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
9
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
9b
d
8 b
d
09b
d
8 b
d
11Phương pháp số 2: Sử dụng The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
8 Nhiệm vụ này có thể được thực hiện bằng hàm The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
9. Đây là chức năng sẵn có trong thư viện The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are ['for', 'best', 'is']
0, trong nội bộ thực hiện nhiệm vụ này và có thể được sử dụng để thực hiện nó bên ngoài. Có nhược điểm của việc in chỉ các phím không giá trị.
3 b
d
The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are ['for', 'best', 'is']
2
5 b
d
The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
9
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
9b
d
8 b
d
09b
d
8 b
d
11Phương pháp số 1:
0b
d
Sự kết hợp của phương pháp trên được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi chỉ cần đảo ngược các giá trị từ điển được thể hiện bằng cách sử dụng
b
d
1 và được truy cập bằng cách sử dụng b
d
2b
d
3 b
d
4b
d
5 b
d
6b
d
7______18import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
0b
d
8 b
d
8import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
4import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
5 import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
6 import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
7import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
8import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
9b
d
8import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
4The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}4
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
6 import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
7The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}7
Đầu ra:
The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
Phương pháp số 2: Sử dụng The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
8 Nhiệm vụ này có thể được thực hiện bằng hàm The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
9. Đây là chức năng sẵn có trong thư viện The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are ['for', 'best', 'is']
0, trong nội bộ thực hiện nhiệm vụ này và có thể được sử dụng để thực hiện nó bên ngoài. Có nhược điểm của việc in chỉ các phím không giá trị.
This task can be performed using the
The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}9 function. This is inbuilt function in
The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are ['for', 'best', 'is']0 library which internally performs this task and can be used to do it externally. Has the drawback of printing just keys not values.
b
d
3 The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are ['for', 'best', 'is']2
b
d
5 The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}9
b
d
7______18import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
0b
d
8 b
d
8import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
4import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
5 import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
6 import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
7import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
8import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
9b
d
8import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
4The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}4
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
6 import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
7The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are {'for': 7, 'is': 4, 'best': 6}7
Đầu ra:
The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4} The top N value pairs are ['for', 'best', 'is']
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Let’s see how to find the second maximum value in a Python dictionary.
Method #1: Naive Approach:
A general approach is to find the largest value of the dictionary and then iterate over the dictionary maintaining the value that must be greater than all the other values and less than the maximum value.
Python3
Bàn luận
Từ điển trong Python giống như các mảng kết hợp hoặc bản đồ trong các ngôn ngữ khác. Không giống như danh sách, từ điển lưu trữ dữ liệu trong cặp giá trị khóa. Xem cách tìm giá trị tối đa thứ hai trong từ điển Python.Method #1: Cách tiếp cận ngây thơ: & NBSP; Cách tiếp cận chung là tìm giá trị lớn nhất của từ điển và sau đó lặp lại Trên từ điển duy trì giá trị phải lớn hơn tất cả các giá trị khác và nhỏ hơn giá trị tối đa.
Các
b
d
37 b
d
8 b
d
37b
d
40b
d
41b
d
8 b
d
43b
d
44 b
d
45b
d
46 b
d
47import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3b
d
60
48b
d
49b
d
50b
d
51 b
d
52b
d
37b
d
54Using sorted[] method b
d
We can find the second largest value in a dictionary by sorting the values of the dictionaries and then retrieving the second last element from the sorted list.
Python3
b
d
61b
d
8 b
d
9b
d
64x = sorted[d.items[],key=[lambda i: i[1]]]
print[x[-2][0]]
print[x[-3][0]]
1b
d
35x = sorted[d.items[],key=[lambda i: i[1]]]
print[x[-2][0]]
print[x[-3][0]]
3__import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
4b
d
82x = sorted[d.items[],key=[lambda i: i[1]]]
print[x[-2][0]]
print[x[-3][0]]
3[4, ['b']]
[3, ['c', 'd']]
3b
d
85b
d
86b
d
87b
d
88Các
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
3import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict[list]
for k,v in d.items[]:
dd[v].append[k]
x = sorted[dd.items[]]
print[x[-2]]
print[x[-3]]
4___Giải thích: example_dict.values [] đưa ra danh sách tất cả các giá trị trong từ điển. Phương thức Sắp xếp [] sẽ sắp xếp danh sách các dict_values. Danh sách [được sắp xếp [example_dict.values []]]] [-2] chuyển đổi dict_values thành danh sách và trả lại phần tử cuối cùng thứ hai của danh sách được sắp xếp, tức là giá trị lớn thứ hai của từ điển. & NBSP; Từ điển Như được hiển thị trong ví dụ thứ 2, việc chuyển đổi các giá trị từ điển thành một tập hợp sẽ loại bỏ các bản sao. & NBSP; example_dict.values[] gives list of all the values in the dictionary. sorted[] method will sort the list of dict_values. list[sorted[example_dict.values[]]][-2] converts dict_values to list and return the second last element of the sorted list, i.e. the second largest value of dictionary.
if more than one maximum values are present in the dictionary as shown in the 2nd example, converting the dictionary values to a set will eliminate the
duplicates.