How to use python sort function
The python sort() function is used to sort the original list. If parameters are specified, the comparison function specified by the comparison function is used.
How to use the python sort function?
sort() method syntax:
list.sort(cmp=None, key=None, reverse=False)
Parameter
cmp -- Optional parameter, if this parameter is specified, the method of this parameter will be used for sorting.
key -- Mainly used for comparison elements, with only one parameter. The parameters of the specific function are taken from the iterable object, and an element in the iterable object is specified for sorting.
reverse -- Sorting rule, reverse = True for descending order, reverse = False for ascending order (default).
Return value
This method has no return value, but it will sort the objects in the list.
Example
The following example shows how to use the sort() function:
Example
#!/usr/bin/python # -*- coding: UTF-8 -*- aList = [123, 'Google', 'Runoob', 'Taobao', 'Facebook']; aList.sort(); print "List : ", aList
The output results of the above example are as follows :
List : [123, 'Facebook', 'Google', 'Runoob', 'Taobao']
The following examples output the list in descending order:
Example
#!/usr/bin/python # -*- coding: UTF-8 -*- # 列表 vowels = ['e', 'a', 'u', 'o', 'i'] # 降序 vowels.sort(reverse=True) # 输出结果 print '降序输出:', vowels
The output results of the above examples are as follows:
降序输出: ['u', 'o', 'i', 'e', 'a']
The following example demonstrates outputting a list by sorting the elements in the specified list:
Example
#!/usr/bin/python # -*- coding: UTF-8 -*- # 获取列表的第二个元素 def takeSecond(elem): return elem[1] # 列表 random = [(2, 2), (3, 4), (4, 1), (1, 3)] # 指定第二个元素排序 random.sort(key=takeSecond) # 输出类别 print '排序列表:', random
The output results of the above example are as follows:
排序列表:[(4, 1), (2, 2), (1, 3), (3, 4)]
Related recommendations:《Python Tutorial》
The above is the detailed content of How to use python sort function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Exploration of cracking verification codes using Python In daily network interactions, verification codes are a common security mechanism to prevent malicious manipulation of automated programs...

How do Python scripts clear output to cursor position at a specific location? When writing Python scripts, it is common to clear the previous output to the cursor position...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
