Hướng dẫn sort nested dictionary by value python descending - sắp xếp từ điển lồng nhau theo giá trị python giảm dần

Tôi có một từ điển một cái gì đó trông như thế này

{
'Host-A': {'requests': 
    {'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
     'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
     'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
'total_hit_count': 9},
}

Như bạn có thể thấy đối với 'Host-A' Giá trị là một dict chứa các yêu cầu nhận được và truy cập đếm trên mỗi trang .. Câu hỏi là làm thế nào để sắp xếp 'requests' theo thứ tự giảm dần. Vì vậy, sau đó tôi có thể nhận được các yêu cầu hàng đầu.

Ví dụ về đầu ra giải pháp chính xác sẽ giống như:

{
'Host-A': {'requests':
    {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
     'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
     'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
'total_hit_count': 9},
}

Tôi đánh giá cao sự giúp đỡ của bạn

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    The combination of above methods can be used to perform this task. In this, we perform the task of sorting using sorted() and lambda and generator expression are used to bind and extracting values of dictionaries.

    test_dict = {'Nikhil' : {

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    0
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    1
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    2

    'Host-A'2'Host-A'3 : {

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    0

    'Host-A'2'requests'8 : {

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    0
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    1__12

    test_dict 2test_dict 3test_dict 4 test_dict 5 test_dict 6test_dict 7

    test_dict 8= =0=1test_dict 3=3__

    {0{1 {2{3 {4

    test_dict 2test_dict 3{7 test_dict 5 test_dict 6'Nikhil'0

    Đầu ra:

    Từ điển ban đầu: {'Nikhil': {'English': 5, 'Toán': 2, 'Khoa học': 14}, 'Akash': {'English': 15, 'Toán': 7, 'Khoa học': 2}, 'akshat': {'English': 5, 'Maths': 50, 'Khoa học': 20}} Từ điển được sắp xếp: Khoa học ': 14},' Akash ': {' Khoa học ': 2,' Toán ': 7,' Tiếng Anh ': 15},' Akshat ': {' English ': 5,' Khoa học ': 20,' Toán ' : 50}}
    The sorted dictionary : {‘Nikhil’: {‘Maths’: 2, ‘English’: 5, ‘Science’: 14}, ‘Akash’: {‘Science’: 2, ‘Maths’: 7, ‘English’: 15}, ‘Akshat’: {‘English’: 5, ‘Science’: 20, ‘Maths’: 50}}

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Phương pháp số 1: Sử dụng 'Nikhil'1

    Nhiệm vụ này có thể được thực hiện bằng cách sử dụng hàm đặt hàng, chuyển đổi từ điển thành thứ tự cụ thể như được đề cập trong các đối số của nó được thao tác bởi hàm được sắp xếp trong đó để sắp xếp theo giá trị của khóa được truyền.

    'Nikhil'2 'Nikhil'3'Nikhil'4 'Nikhil'5

    'Nikhil'2 'Nikhil'7'Nikhil'4 'Nikhil'9

    test_dict = {'Nikhil' : {4: {5

    'Host-A'2'requests'8 : {: {5

    'Host-A'2'Host-A'3 : {4: {5

    test_dict 2test_dict 3test_dict 4 test_dict 5 test_dict 6test_dict 7

    test_dict 8=

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    33=3
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    35

    {0

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    37= =6
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    40=8
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    42: {9
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    44

    test_dict 2test_dict 3

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    47 test_dict 5 test_dict 6
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    50

    Đầu ra:

    Từ điển ban đầu: {'Nikhil': {'roll': 24, 'Marks': 17}, 'akash': {'roll': 12, 'Marks': 15}, 'akshat': {'roll': 54, 'Marks': 12}} Từ điển được sắp xếp theo điểm là: OrderedDict ([('akshat', {'roll': 54, 'Marks': 12}), ('akash', {'roll': 12 , 'Marks': 15}), ('Nikhil', {'roll': 24, 'Marks': 17})]))
    The sorted dictionary by marks is : OrderedDict([(‘Akshat’, {‘roll’: 54, ‘marks’: 12}), (‘Akash’, {‘roll’: 12, ‘marks’: 15}), (‘Nikhil’, {‘roll’: 24, ‘marks’: 17})])

    Phương pháp số 2: Sử dụng sorted()

    Chúng ta có thể đạt được kết quả trên tốt hơn nếu chúng ta chỉ sử dụng hàm được sắp xếp vì nó trả về kết quả ở định dạng có thể sử dụng hơn, Dict và thực hiện chính xác nhiệm vụ mong muốn.

    test_dict = {'Nikhil' : {4: {5

    'Host-A'2'requests'8 : {: {5

    'Host-A'2'Host-A'3 : {4: {5

    test_dict 2test_dict 3test_dict 4 test_dict 5 test_dict 6test_dict 7

    test_dict 8=

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    33=3
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    35

    test_dict 2test_dict 3

    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    47 test_dict 5 test_dict 6
    {
    'Host-A': {'requests':
        {'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
         'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
         'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
    'total_hit_count': 9},
    }
    
    50

    Đầu ra:

    Từ điển ban đầu: {'Nikhil': {'roll': 24, 'Marks': 17}, 'akash': {'roll': 12, 'Marks': 15}, 'akshat': {'roll': 54, 'Marks': 12}} Từ điển được sắp xếp theo điểm là: OrderedDict ([('akshat', {'roll': 54, 'Marks': 12}), ('akash', {'roll': 12 , 'Marks': 15}), ('Nikhil', {'roll': 24, 'Marks': 17})]))
    The sorted dictionary by marks is : [(‘Akshat’, {‘marks’: 12, ‘roll’: 54}), (‘Akash’, {‘marks’: 15, ‘roll’: 12}), (‘Nikhil’, {‘marks’: 17, ‘roll’: 24})]


    Làm thế nào để bạn sắp xếp một từ điển theo giá trị giảm dần trong Python?

    Đảo ngược với giá trị của True sẽ sắp xếp từ điển được sắp xếp theo thứ tự giảm dần. Bạn có thể thấy đầu ra bị đảo ngược vì chúng tôi đã chuyển ngược lại = true với phương thức Sắp xếp (). Nếu bạn không đặt ngược lại hoặc bạn đặt giá trị của nó thành sai, từ điển sẽ được sắp xếp theo thứ tự tăng dần. Đó là mặc định.. You can see the output is reversed because we passed reverse=True to the sorted() method. If you don't set reverse at all or you set its value to false, the dictionary will be arranged in ascending order. That's the default.

    Làm thế nào để bạn sắp xếp tăng dần và giảm dần một từ điển theo giá trị trong Python?

    Sử dụng Dict.Items () để có được danh sách các cặp Tuple từ D và sắp xếp nó bằng hàm Lambda và Sắp xếp ().Sử dụng Dict () để chuyển đổi danh sách được sắp xếp trở lại từ điển.Sử dụng tham số ngược trong Sắp xếp () để sắp xếp từ điển theo thứ tự ngược lại, dựa trên đối số thứ hai. items() to get a list of tuple pairs from d and sort it using a lambda function and sorted(). Use dict() to convert the sorted list back to a dictionary. Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the second argument.

    Làm thế nào để bạn sắp xếp một từ điển từ thấp nhất đến cao nhất trong Python?

    Sắp xếp từ điển theo các mục giá trị () cho hàm tích hợp được sắp xếp () và đặt đối số khóa từ khóa sắp xếp của nó thành hàm lambda.Chúng tôi cũng đã đặt đối số từ khóa đảo ngược = true để điểm số được sắp xếp theo thứ tự giảm dần từ cao nhất đến thấp nhất.Lưu ý: Nếu bạn đang sử dụng Python 2. X, bạn có thể vượt qua điểm số.items() to the sorted() built-in function and set its sorting key keyword argument to a lambda function. We've also set the reverse=True keyword argument so that the scores are sorted in descending order from highest to lowest. Note: If you're using Python 2. x you can pass scores.

    Làm thế nào để bạn sắp xếp một từ điển theo giá trị trong Python?

    Làm thế nào để sắp xếp từ điển theo giá trị trong Python ?..
    Sử dụng một vòng lặp ..
    Sử dụng hàm Sắp xếp () ..
    Sử dụng chức năng Lambda ..
    Sử dụng phương thức từ điển.items () ..