Hướng dẫn nth highest value in dictionary python - giá trị cao thứ n trong từ điển python

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ị.

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

  • 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]]
    
    9
    b
    d
    
    8
    b
    d
    
    09
    b
    d
    
    8
    b
    d
    
    11
  • Cải thiện bài viết
  • 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ị.

    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

    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]]
    
    9
    b
    d
    
    8
    b
    d
    
    09
    b
    d
    
    8
    b
    d
    
    11

    Phương pháp số 1:

    b
    d
    
    0

    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
    
    2

    b
    d
    
    3
    b
    d
    
    4
    b
    d
    
    5
    b
    d
    
    6

    b
    d
    
    7______18

    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]]
    
    0
    b
    d
    
    8
    b
    d
    
    8

    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]]
    
    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]]
    
    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]]
    
    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]]
    
    7
    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]]
    
    8

    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]]
    
    9
    b
    d
    
    8

    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]]
    
    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]]
    
    4
    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}
    
    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]]
    
    7
    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}
    
    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______18

    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]]
    
    0
    b
    d
    
    8
    b
    d
    
    8

    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]]
    
    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]]
    
    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]]
    
    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]]
    
    7
    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]]
    
    8

    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]]
    
    9
    b
    d
    
    8

    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]]
    
    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]]
    
    4
    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}
    
    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]]
    
    7
    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}
    
    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
  • Bàn luận
  • 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
    
    37
    b
    d
    
    40

    b
    d
    
    41
    b
    d
    
    8
    b
    d
    
    43

    b
    d
    
    44
    b
    d
    
    45
    b
    d
    
    46
    b
    d
    
    47

    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]]
    
    3
    b
    d
    
    60

    b
    d
    
    48
    b
    d
    
    49
    b
    d
    
    50
    b
    d
    
    51
    b
    d
    
    52
    b
    d
    
    37
    b
    d
    
    54
    Using sorted[] method 
    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
    
    61
    b
    d
    
    8
    b
    d
    
    9
    b
    d
    
    64
    x = sorted[d.items[],key=[lambda i: i[1]]]
    print[x[-2][0]]
    print[x[-3][0]]
    
    1
    b
    d
    
    35
    x = 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]]
    
    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]]
    
    4
    b
    d
    
    82
    x = sorted[d.items[],key=[lambda i: i[1]]]
    print[x[-2][0]]
    print[x[-3][0]]
    
    3
    [4, ['b']]
    [3, ['c', 'd']]
    
    3
    b
    d
    
    85
    b
    d
    
    86
    b
    d
    
    87
    b
    d
    
    88

    Cá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]]
    
    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]]
    
    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. 


    Bài Viết Liên Quan

    Chủ Đề