Introduction to Python Programming (016) - Dictionary Operations (3): Dictionary Sorting

Introduction to Python Programming (016) - Dictionary Operations (3): Dictionary Sorting

1. sorted() function

The sorted() function can sort an iterable object based on the provided function and ascending and descending order flags. The syntax format of the sorted() function is as follows:

sorted(可迭代对象, key = 函数名, reverse = False/True)

illustrate:

(1) key: Set the sorting basis. Usually, you can return the sorting basis by function, and then bind the function name to the key.

(2)reverse: sorting direction. The default is False, indicating ascending order. When set to True, it indicates descending order.

For example:

test = ["107","88","211","985","1234","6879","55"]
print(sorted(test))    # 按照字符串升序排列
print(sorted(test, key=int))   # 按照整数升序排列
print(sorted(test, key=lambda x:int(x)))     # 按照整数升序排列
print(sorted(test, key=int, reverse=True))   # 按照整数降序排列
lang = ["Java","Python","JavaScript","C#","PHP"]
print(sorted(lang))          
print(sorted(lang,key=len))  # 按字符串长度排序
print(sorted(lang,key=lambda x:len(x),reverse=True))  # 按字符串长度降序排列

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
['107', '1234', '211', '55', '6879', '88', '985']
['55', '88', '107', '211', '985', '1234', '6879']
['55', '88', '107', '211', '985', '1234', '6879']
['6879', '1234', '985', '211', '107', '88', '55']
['C#', 'Java', 'JavaScript', 'PHP', 'Python']
['C#', 'PHP', 'Java', 'Python', 'JavaScript']
['JavaScript', 'Python', 'Java', 'PHP', 'C#']

2. Sort the dictionary directly

Use the sorted() function to sort the dictionary directly. After sorting, a list containing only dictionary keys is generated. The default keys are arranged in ascending order. If you want to sort the keys in descending order, set the parameter reverse to True.

For example:

test = {
    
    12:"Tom",102:"Jerry",88:"Jack",125:"Mark",27:"Black",35:"White"}
item1 = sorted(test)
print(type(item1))
print(item1)
print(sorted(test,reverse=True))  # 降序排列

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
<class 'list'>
[12, 27, 35, 88, 102, 125]
[125, 102, 88, 35, 27, 12]

3. Sort dictionary keys or values

Sort the keys or values ​​of the dictionary. After sorting, a list containing only the keys or values ​​of the dictionary will be generated. By default, the list is sorted in ascending order. If you want to sort the keys in descending order, set the parameter reverse to True.

For example:

test = {
    
    12:"Tom",102:"Jerry",88:"Jack",125:"Mark",27:"Black",35:"White"}
item1 = sorted(test.keys())    # 按字典的键排序
print(type(item1))
print(item1)
item2 = sorted(test.values())  # 按字典的值排序
print(type(item2))
print(item2)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
<class 'list'>
[12, 27, 35, 88, 102, 125]
<class 'list'>
['Black', 'Jack', 'Jerry', 'Mark', 'Tom', 'White']

4. Sort the key-value pairs (items) of the dictionary

(1) When sorting the key-value pairs (items) of the dictionary, the default is to sort by key in ascending order (if you want to sort by key in descending order, set the parameter reverse to True), and output a list containing keys and values. Keys and values ​​are presented as tuples.

For example:

test = {
    
    12:"Tom",102:"Jerry",88:"Jack",125:"Mark",27:"Black",35:"White"}
item1 = sorted(test.items())    # 按字典的键排序
print(type(item1))
print(item1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
<class 'list'>
[(12, 'Tom'), (27, 'Black'), (35, 'White'), (88, 'Jack'), (102, 'Jerry'), (125, 'Mark')]

(2) If you want to sort by the value of the dictionary, you can use the anonymous function lambda to set the sorting dictionary to the value.

For example:

test = {
    
    12:"Tom",102:"Jerry",88:"Jack",125:"Mark",27:"Black",35:"White"}
item1 = sorted(test.items(),key=lambda x:x[1])    # 按字典的值排序
print(type(item1))
print(test)
print(item1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
<class 'list'>
{
    
    12: 'Tom', 102: 'Jerry', 88: 'Jack', 125: 'Mark', 27: 'Black', 35: 'White'}
[(27, 'Black'), (88, 'Jack'), (102, 'Jerry'), (125, 'Mark'), (12, 'Tom'), (35, 'White')]

5. Convert dictionary to list for sorting

Convert the dictionary's key-value pairs to a list and then sort them using the sorted() or sort() function and the lambda function.

For example:

test = {
    
    12:"Tom",102:"Jerry",88:"Jack",125:"Mark",27:"Black",35:"White"}
list1 = list(test.items())
print(sorted(list1,key=lambda x:x[1]))

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[(27, 'Black'), (88, 'Jack'), (102, 'Jerry'), (125, 'Mark'), (12, 'Tom'), (35, 'White')]

After converting to a list, you can use the sort() method of the list to sort.

For example:

test = {
    
    12:"Tom",102:"Jerry",88:"Jack",125:"Mark",27:"Black",35:"White"}
list1 = list(test.items())
list1.sort(key=lambda x:x[1])
print(list1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[(27, 'Black'), (88, 'Jack'), (102, 'Jerry'), (125, 'Mark'), (12, 'Tom'), (35, 'White')]

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132186881