Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọchow to sort a dictionary by value in Python.
Bàn luận
Trong bài viết này, chúng tôi sẽ đề cập đến cách sắp xếp từ điển theo giá trị trong Python. & NBSP;
Sắp xếp luôn là một tiện ích hữu ích trong lập trình hàng ngày. Từ điển trong Python được sử dụng rộng rãi trong nhiều ứng dụng từ miền cạnh tranh đến miền nhà phát triển [ví dụ: xử lý dữ liệu JSON]. Có kiến thức để sắp xếp từ điển theo giá trị của họ có thể chứng minh hữu ích trong những trường hợp như vậy.
Có 2 cách để đạt được sự sắp xếp này:sorted[]” inbuilt function. Various variations can also be achieved for sorting the dictionaries.
- Hàm Lambda trong Python là gì? Use “reverse = True” in addition to the sorted[] function.
- Bài viết này liên quan đến việc sắp xếp bằng hàm Lambda và sử dụng chức năng sẵn có được sắp xếp []. Các biến thể khác nhau cũng có thể đạt được để sắp xếp các từ điển. & NBSP; Separate by “comma” mentioning the correct order in which sorting has to be performed.
Example:
Python3
Đối với thứ tự giảm dần: Sử dụng Reverse Reversion = true, ngoài hàm Sắp xếp [].
from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=operator.itemgetter[1]]] print["Sorted Dictionary by value: ", sort_dict]0
from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=operator.itemgetter[1]]] print["Sorted Dictionary by value: ", sort_dict]1
operator.itemgetter[value]2
operator.itemgetter[value]3
from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=operator.itemgetter[1]]] print["Sorted Dictionary by value: ", sort_dict]4
operator.itemgetter[value]5
operator.itemgetter[value]6
operator.itemgetter[value]3
operator.itemgetter[value]8
operator.itemgetter[value]9
Để sắp xếp W.R.T nhiều giá trị: tách biệt bởi dấu phẩy, đề cập đến thứ tự chính xác trong đó việc sắp xếp phải được thực hiện.
lambda arguments: expression0
lambda arguments: expression1
lambda arguments: expression2
lambda arguments: expression3
Is
lambda arguments: expression0
lambda arguments: expression1
inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=lambda item: item[1]]] print["Sorted Dictionary by value: ", sort_dict]7
lambda arguments: expression3
lambda arguments: expression0
lambda arguments: expression1
Dictionary: {'a': 3, 'ab': 2, 'abc': 1, 'abcd': 0} Sorted Dictionary by value: {'abcd': 0, 'abc': 1, 'ab': 2, 'a': 3}1
lambda arguments: expression3
from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=operator.itemgetter[1]]] print["Sorted Dictionary by value: ", sort_dict]0
from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=operator.itemgetter[1]]] print["Sorted Dictionary by value: ", sort_dict]1______12
lambda arguments: expression0
lambda arguments: expression1
inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=lambda item: item[1]]] print["Sorted Dictionary by value: ", sort_dict]7
lambda arguments: expression3
lambda arguments: expression0
lambda arguments: expression1
Dictionary: {'a': 3, 'ab': 2, 'abc': 1, 'abcd': 0} Sorted Dictionary by value: {0: 'abcd', 1: 'abc', 2: 'ab', 3: 'a'}2
lambda arguments: expression3
Các
Output:
The list printed sorting by age: [{'age': 19, 'name': 'Nikhil'}, {'age': 20, 'name': 'Nandini'}, {'age': 20, 'name': 'Manjeet'}] The list printed sorting by age and name: [{'age': 19, 'name': 'Nikhil'}, {'age': 20, 'name': 'Manjeet'}, {'age': 20, 'name': 'Nandini'}] The list printed sorting by age in descending order: [{'age': 20, 'name': 'Nandini'}, {'age': 20, 'name': 'Manjeet'}, {'age': 19, 'name': 'Nikhil'}]
Các Ways to sort list of dictionaries by values in Python – Using itemgetter
Này, các bạn! Trong bài viết này, chúng tôi sẽ tiết lộ các bước để sắp xếp một từ điển theo giá trị trong Python. Mô-đun toán tử Python có các chức năng được xây dựng khác nhau để thao tác dữ liệu và giá trị. Phương thức Toán tử Cú pháp: Toán tử Python.Itemgetter [M]Kỹ thuật 1: Sử dụng hàm Sắp xếp [] để sắp xếp từ điển theo giá trị trong Python
list
8 cùng với Python list
9 có thể được sử dụng để sắp xếp một từ điển theo giá trị trong Python.operator.itemgetter[value]
Python Sắp xếp phương thức [] Sắp xếp các dict theo thứ tự tăng dần/giảm dần.
Cú pháp: Phương thức Python Sắp xếp []
operator.itemgetter[value]
00: Nó đại diện cho các yếu tố được sắp xếp.- ________ 101 [Tùy chọn]: Một chức năng để quyết định thứ tự sắp xếp các yếu tố.
Example:
from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=operator.itemgetter[1]]] print["Sorted Dictionary by value: ", sort_dict]
Trong hàm trên, chúng tôi đã đặt phím tham số = toán tử.itemetter [1] bởi vì ở đây ‘1, đại diện cho các giá trị của dict và‘ 0, đại diện cho các khóa của dict. Vì vậy, chúng tôi đã sắp xếp các dict bằng các giá trị theo thứ tự tăng dần.
Output:
Dictionary: {'a': 3, 'ab': 2, 'abc': 1, 'abcd': 0} Sorted Dictionary by value: {'abcd': 0, 'abc': 1, 'ab': 2, 'a': 3}
Kỹ thuật 2: Hàm Lambda Python cùng với Phương thức Sắp xếp []
Phương thức Python Sắp xếp [] có thể được câu lạc bộ cùng với hàm Lambda để sắp xếp một từ điển theo giá trị trong Python theo thứ tự được xác định trước.
Chức năng Python Lambda tạo ra một hàm ẩn danh, tức là một hàm không có tên. Nó giúp tối ưu hóa mã.
Cú pháp: Chức năng Python Lambda
lambda arguments: expression
Example:
inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[inp_dict.items[], key=lambda item: item[1]]] print["Sorted Dictionary by value: ", sort_dict]
Trong đoạn mã trên, chúng tôi đã tạo ra một hàm lambda và chuyển các giá trị của dict như đối số cho nó bằng cách lặp qua dict bằng các giá trị, tức là mục [1].
Output:
Dictionary: {'a': 3, 'ab': 2, 'abc': 1, 'abcd': 0} Sorted Dictionary by value: {'abcd': 0, 'abc': 1, 'ab': 2, 'a': 3}
Kỹ thuật 3: Phương thức Python Sắp xếp [] cùng với Dict.Items []
Hàm python sort [] có thể được sử dụng để sắp xếp từ điển theo giá trị trong python bằng cách truyền các giá trị thông qua dict.items [] cho phương thức.
operator.itemgetter[value]02 lấy các phím/giá trị từ một từ điển.
Syntax:
Example:
inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print["Dictionary: ", inp_dict] sort_dict= dict[sorted[[value, key] for [key,value] in inp_dict.items[]]] print["Sorted Dictionary by value: ", sort_dict]
Trong đoạn mã trên, chúng tôi đã chuyển cặp [giá trị, khóa] cho hàm được sắp xếp và đã tìm nạp các giá trị dict bằng phương thức Dict.Items []. Trong trường hợp này, chúng tôi sẽ nhận được cặp được sắp xếp [giá trị, khóa] làm đầu ra.
Output:
Dictionary: {'a': 3, 'ab': 2, 'abc': 1, 'abcd': 0} Sorted Dictionary by value: {0: 'abcd', 1: 'abc', 2: 'ab', 3: 'a'}
Sự kết luận
Do đó, trong bài viết này, chúng tôi đã hiểu các cách khác nhau để sắp xếp một từ điển theo giá trị trong Python.
Người giới thiệu
- Python sắp xếp dict bằng các giá trị - Tạp chí