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!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function